/* Test the Shrimpware LLC BreadBox analog inputs and servos Rev 1.0 When running switch 5 will enable/disable servo movement. switch 6 will begin/end a slow sweep by servo 10 servos 3,5,6,9 will follow the position of potentiometers 1-4 */ #include // yes, these variables could be in an array, but I leave them // separate to make the code easier for a newer programmer to read // one variable for each servo Servo myservo3; Servo myservo5; Servo myservo6; Servo myservo9; Servo myservo10; Servo myservo11; // where the servo is now int servo3Position = 0; int servo5Position = 0; int servo6Position = 0; int servo9Position = 0; int servo10Position = 0; int servo11Position = 0; // when true, the servo10 will continue to execute a sweep boolean servo10TestRunning = false; const int c_DEBOUNCE_DELAY = 100; // switches must be held this long to be considered a change long programStartMillis = 0; // clock time when the program is started // To hold the last read pin values int a0ReadValue = 0; int a1ReadValue = 0; int a2ReadValue = 0; int a3ReadValue = 0; int a4ReadValue = 0; int a5ReadValue = 0; // To hold the previous values of each analog input int a0Previous = 0; int a1Previous = 0; int a2Previous = 0; int a3Previous = 0; //Button / switch // LastReadValue is the analog input value which State is the current debounced state of the switch boolean button4LastReadValue; boolean button4State = LOW; long button4LastDebounceTime = 0; boolean switch5LastReadValue; boolean switch5State = LOW; long switch5LastDebounceTime = 0; void printHelp() { // called when the Arduino is reset. To see this open Tools -> Serial Monitor // and set the speed to 57600. Then press the Arduino reset. Serial.println("Breadbox Test Program Revision 2."); Serial.println("Type a space in the terminal monitor and click send to display debug values."); } // a utility routine to print how long the program has been running void printTimeWithReturn(bool withReturn) { long runningTime = millis() - programStartMillis; Serial.print(runningTime); if (withReturn) Serial.println(); } // Called every Loop, this prints out an "I'm alive" message every 5 seconds void heartbeat () { static long lastbeat = 0; long currentTime = millis(); if (currentTime - lastbeat > 5000) { lastbeat = currentTime; printTimeWithReturn(false); Serial.println(" heartbeat"); } } // When called will printout stuff. If you want to debug something put // print statements in here. void printDebug() { printServoPositions(); printAnalogInputValues(); } void printServoPositions() { // shows where the program last told each servo to be if (servo10TestRunning) Serial.println("servo10TestRunning"); Serial.println("servo positions: 3, 5, 6, 9, 10, 11"); Serial.print (" "); Serial.print(servo3Position); Serial.print(", "); Serial.print(servo5Position); Serial.print(", "); Serial.print(servo6Position); Serial.print(", "); Serial.print(servo9Position); Serial.print(", "); Serial.print(servo10Position); Serial.print(", "); Serial.print(servo11Position); Serial.println(""); } void printAnalogInputValues() { // prints the last value read for each analog pin Serial.println("analog values: 0, 1, 2, 3, 4, 5"); Serial.print (" "); Serial.print(a0ReadValue); Serial.print(", "); Serial.print(a1ReadValue); Serial.print(", "); Serial.print(a2ReadValue); Serial.print(", "); Serial.print(a3ReadValue); Serial.print(", "); Serial.print(a4ReadValue); Serial.print(", "); Serial.print(a5ReadValue); Serial.println (""); } // called each Loop to see if Servo10 needs to move void checkServo10Test() { static long lastMoveTime; long timeSinceLastMove = millis() - lastMoveTime; // only move position every 15 miliseconds if (timeSinceLastMove > 15) { lastMoveTime = millis(); static boolean goingUp = true; if (servo10Position >= 180) goingUp = false; if (servo10Position <= 0) goingUp = true; if (goingUp) { servo10Position++; } else { servo10Position--; } myservo10.write(servo10Position); } } // this routine will debounce a switch or button, only allowing it to change // state if the new value has been held for some time void buttonStateCheck(int valueRead, boolean *stateLast, boolean *stateCurrent, long *timeLastChange, char* tag) { long currentTime = millis(); // what state is the value currently? boolean currentReadState = LOW; if (valueRead > 900) currentReadState = HIGH; // has the input changed state? if (currentReadState != *stateLast) { // it has so set the debounce timer to now *timeLastChange = currentTime; *stateLast = currentReadState; } if ((currentTime - *timeLastChange) > c_DEBOUNCE_DELAY) { //current state has been stable longer than debounce limit if (*stateCurrent != currentReadState) { //so change the state *stateCurrent = currentReadState; printTimeWithReturn(false); Serial.print(" Switch "); Serial.print(tag); Serial.print(" changed to: "); Serial.println(*stateCurrent); } } } // the setup routine runs once when you press reset: void setup() { programStartMillis = millis(); // initialize serial communication at 9600 bits per second: Serial.begin(57600); // initialze the servo objects to their digital pins myservo3.attach(3); myservo5.attach(5); myservo6.attach(6); myservo9.attach(9); myservo10.attach(10); myservo11.attach(11); // initialize the analog pins for input pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT); pinMode(A4, INPUT); digitalWrite(A4, HIGH); //set the pull up resistor pinMode(A5, INPUT); digitalWrite(A5, HIGH); //set the pull up resistor printHelp(); } // the loop routine runs over and over again forever: void loop() { heartbeat(); // if the user sends in a space via the terminal, print out debug info if (Serial.available() > 0) { // get incoming byte: int inByte = Serial.read(); if (inByte = 32) printDebug(); } // read the input on analog pins a0ReadValue = analogRead(A0); a1ReadValue = analogRead(A1); a2ReadValue = analogRead(A2); a3ReadValue = analogRead(A3); a4ReadValue = analogRead(A4); a5ReadValue = analogRead(A5); // debounce button 4 buttonStateCheck(a4ReadValue, &button4LastReadValue, &button4State, &button4LastDebounceTime, "4"); // if button 4 has changed state to true, toggle the servo10 test static boolean button4LastState = false; if (button4LastState != button4State) { // button4 changed state button4LastState = button4State; if (!button4State) servo10TestRunning = !servo10TestRunning; } // check switch 5. If off, do nothing. // if on, then let the servos follow the potentiometers and pump the servo 10 sweep buttonStateCheck(a5ReadValue, &switch5LastReadValue, &switch5State, &switch5LastDebounceTime, "5"); if (switch5State) { if (servo10TestRunning) checkServo10Test(); int servoPosition; servo3Position = map(a0ReadValue, 0,1023,0,179); myservo3.write(servo3Position); servo5Position = map(a1ReadValue, 0,1023,0,179); myservo5.write(servo5Position); servo6Position = map(a2ReadValue, 0,1023,0,179); myservo6.write(servo6Position); servo9Position = map(a3ReadValue, 0,1023,0,179); myservo9.write(servo9Position); //myservo11... what *do* you want servo 11 to do? Good luck! } //end of switch5State }