//Servo Board //SAMD11C int servo = 9; int angle; int pwm; int button = 2; int buttonFlag = 0; int lastState = 1; int monitorOpen = 0; int oldCount = 0; int newCount; int progress; String message; const char endDelimiter = '>'; void setup() { pinMode(button, INPUT); pinMode(servo, OUTPUT); Serial.begin(115200); Serial2.begin(115200); } void loop() { if(Serial && monitorOpen == 0){ monitorOpen = 1; Serial.println("Use the Serial Monitor to input current wordcount or use the button to record progress in a project."); } if (Serial.available()){ // If anything comes in Serial (USB) newCount = Serial.parseInt(); //Read user input and hold it in a variable Serial2.print (newCount); // send the input over Serial 2 Serial2.print (endDelimiter); delay (1000); progress = newCount - oldCount; Serial.print("Old wordcount: "); Serial.println(oldCount); Serial.print("New wordcount: "); Serial.println(newCount); Serial.print("Progress: "); Serial.print(progress); Serial.println(" words"); oldCount = newCount; if (progress > 0){ delay (500); activateServo(); } } else if (digitalRead(button) == LOW && lastState == 1){ message = "Button>"; Serial2.write(message.c_str()); //Send message over Serial2 lastState = 0; delay (500); activateServo(); if (buttonFlag == 0) { buttonFlag = 1; } else if(buttonFlag == 1){ digitalWrite(servo, LOW); delay(50); buttonFlag = 0; } } else { lastState = 1; } } void servoPulse (int servo, int angle) { pwm = (angle*11) + 500; // Convert angle to microseconds digitalWrite(servo, HIGH); delayMicroseconds(pwm); digitalWrite(servo, LOW); delay(50); // Refresh cycle of servo } void activateServo () { for (angle = 43; angle >= 0; angle -= 1) { servoPulse(servo, angle); } delay(5000); for (angle = 0; angle <= 43; angle += 1) { servoPulse(servo, angle); } }