// EDIT THIS LINE ******************************** String programName = "servoTest01"; // from BareMinimum12 long baudRateSer = 9600; // ------------------------------------- /* program from arduino.cc Controlling a servo position using a potentiometer by Michal Rinott modified on 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Knob */ // Purpose: // this original program was written for servos that are NOT 360deg continuous rotation // in that case, the pot position controls the servo position. // However in my case I'm using a 360deg continuous rotation servo, // in which case pot position controls the speed of servo rotation // // // Edit History: // rev. 01: orig program from arduino.cc // // // Program operation/flow: // blink function for setupLED at start & end of setup // heartbeat function (first task in loop) // turn off any LED's previously set for some intended duration (second task in loop) // Serial is started to print preamble with filename of the program/sketch currently loaded // also prints out MCU info: board model (eg. nano), ID#, and comments field // // Using a 360deg continuous rotation servo: // position of pot controls speed of rotation // if pot wiper is centered, rotation is stopped // turning pot one direction from center will rotate servo CW, // and turning it the other direction will rotate servo CCW // // Eeprom memory configuration: // address 0 & 1 are two bytes identifying the arduino model: // NN = Nano // UN = Uno // MG = Mega // address 2 - 4 are 3 bytes of ID# // address 5 is 1 byte of length of comments // address 6+ is comment // // // Hardware configuration: // 13 onboard LED // 10k (nom) pot connected between +5 & gnd from Arduino // 0 analog input pin for pot wiper // servo motor connected between // Arduino gnd and +5 from external supply (for high current needed for motor) // Arduino gnd and external supply gnd tied together // 9 digital output to control servo // // ------------------------ // ************** INCLUDES ******************* #include #include // ---------- END includes ------------ // ************** GLOBALS ******************* Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin unsigned long currentTime = millis(); // for HB and any other LED to be turned on for some amount of time // for heartbeat: int HBLED = 13; unsigned long LASTHBLED = millis(); // ------------------------------- // for setup blinks: int setupLED = 13; // ------------------------------- // add a line like this (here in Globals) for any LEDs that will be turned off with turnOffLED function // unsigned long lastTestLED = millis(); // to calculate on-duration // call turnoff function with this: // turnOffLED(LEDtoTurnOff, LASTonFlag, onDuration) // ---------------- END globals ----------------- // ************** SETUP ******************* void setup() { pinMode(setupLED, OUTPUT); // for setup blinks pinMode(HBLED, OUTPUT); // for heartbeat // ----------------- // at start of setup: blink setupLED // blinkLED (LEDtoBlink, prePause, numBlinks, blinkOnTime, betweenBlinks, postPause); // blink 4 shorts blinkLED (setupLED, 500, 4, 200, 160, 500); // ------------------- printPreamble(); readMCUinfo(); // ---------------------------------- myservo.attach(9); // attaches the servo on pin 9 to the servo object // ---------------------------------- // at end of setup: blink boardLED // blinkLED (LEDtoBlink, prePause, numBlinks, blinkOnTime, betweenBlinks, postPause); // blink 2 longs blinkLED (setupLED, 500, 2, 90, 160, 1500); } // -------------- END setup -------------- // ************** LOOP ******************* void loop() { // to disable heartbeat, comment out next line: heartBeat(); // --------------------------- // turn off LED's: turnOffLED(HBLED, LASTHBLED, 100); // for heartbeat on-duration // --------------------------- // Serial.println("Starting loop..."); val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } // --------------- END loop --------------- // **************** FUNCTION DEFS ***************** // list of function defs: // readMCUinfo // turnOffLED // blinkLED // heartBeat // printPreamble // -------------------------- void readMCUinfo() { // reads model code, ID#, and comments: int modelAddr = 0; int IDAddr = 2; int commentAddr = 5; // read model: char modelChar1 = EEPROM.read(modelAddr); char modelChar2 = EEPROM.read(modelAddr+1); Serial.print("model is: "); Serial.print(modelChar1); Serial.println(modelChar2); // ---------------------- // read ID: char IDChar1 = EEPROM.read(IDAddr); char IDChar2 = EEPROM.read(IDAddr+1); char IDChar3 = EEPROM.read(IDAddr+2); Serial.print("ID is: "); Serial.print(IDChar1); Serial.print(IDChar2); Serial.println(IDChar3); // ---------------------- // read comments: byte commentLength = EEPROM.read(commentAddr); Serial.print("Comment is: "); Serial.print(commentLength); Serial.println(" chars long"); for (int n = 1; n < commentLength+1; n++) { char inChar = EEPROM.read(commentAddr + n); Serial.print(inChar); } // endfor Serial.println(""); Serial.println("----------"); } // ----------------- END readMCUinfo ---------------------- void turnOffLED (int LEDtoTurnOff, unsigned long LASTon, int timeOn) { // turns off LED's that have been elsewhere set to be on // when the desire is that they stay on for only a defined amount of time (eg. a blink) // takes parameter LEDtoTurnOff as output pin to turn off // takes parameter LASTon as millis() time the LED was last turned on // takes parameter timeOn as millis duration for LED to be on before turning it off // requires that LEDtoTurnOff be defined as OUTPUT // requires that LED be wired through resistor to ground (ie. High active=on) // requires that currentTime be defined in globals as: // unsigned long currentTime = millis(); // requires that variable LAST[pin name variable] (eg. LASTgrnLED): // be defined in globals as: eg. unsigned long LASTgrnLED = millis(); // and that it be set to millis() whenever LED is turned on in the code // this function is usually called as second task in loop, after heartbeat // called for each LED that might need turning off // example calling protocol: // turnOffLED(grnLED, LASTgrnLED, 100); currentTime = millis(); if (currentTime > (LASTon + timeOn)) { digitalWrite(LEDtoTurnOff, LOW); } // endif } // ----------------- END turnOffLED ---------------------- void blinkLED (int LEDtoBlink, int prePause, int numBlinks, int blinkOn, int betweenBlinks, int postPause) { // blinks some LED // typically used to blink board or setup LED to indicate start & end of setup routine // takes parameters: LEDtoBlink; pre-pause; number of blinks; blinkOn duration; betweenBlink pause duration; post-pause // downside to this function is that it ties up the processor for duration of the blink // see instead how the heartbeat function uses turnOffLED function to avoid this delay(prePause); for (int n = 0; n < numBlinks; n++) { digitalWrite(LEDtoBlink, HIGH); delay(blinkOn); digitalWrite(LEDtoBlink, LOW); delay(betweenBlinks); } // endfor delay(postPause); } // ----------------- END blinkLED ---------------------- void heartBeat() { // usually called as first task in loop // requires HBLED defined as output and connected to an LED (which is connected to gnd) // requires variables LASTHBLED & currentTime defined in globals as: // unsigned long LASTHBLED = millis(); // for heartbeat // unsigned long currentTime = millis(); // this version just turns on the HB LED when it's time, // and it gets turned off with turnOffLED function currentTime = millis(); if (currentTime > (1000 + LASTHBLED)) { //blinks every 1000ms digitalWrite(HBLED, HIGH); LASTHBLED = currentTime; } // endif } // ---------------- END heartBeat ---------------- void printPreamble() { Serial.begin(baudRateSer); delay(100); Serial.println(" "); Serial.println(" ---------- "); Serial.print("Program name: "); Serial.println(programName); Serial.println(" ---------- "); } // ---------------- END printPreamble ---------------- // --------------- END function defs ------------ // --------------- END program ------------------