/* Battery Test Serial Writer * Sends a serial heartbeat that indicates that battery power * is still sufficient. When the battery dies, the heartbeat will stop */ int ledPin = 13; // select the pin for the LED int servoPin = 2; // Control pin for servo motor int minPulse = 500; // Minimum servo position int maxPulse = 2500; // Maximum servo position int pulse = 0; // Amount to pulse the servo unsigned long lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 20; // the time needed in between pulses int analogValue = 0; // the value returned from the analog sensor int analogPin = 0; // the analog pin that the sensor's on unsigned long startTime = 0; boolean moveUp = false; void setup() { pinMode(ledPin,OUTPUT); // declare the LED's pin as output pinMode(servoPin, OUTPUT); // Set servo pin as an output pin pulse = minPulse; // Set the motor position value to the minimum beginSerial(9600); // connect to the serial port blinkLED(ledPin, 2, 100); } void loop() { unsigned long now = millis(); if (now - startTime > 1000) { // analogValue = analogRead(analogPin); // read the analog input Serial.print("now: "); Serial.print(now, DEC); Serial.print(" startTime: "); Serial.println(startTime, DEC); if (moveUp == true) { analogValue = 768; pulse = (analogValue * 19) / 10 + minPulse; // convert the analog value moveUp = false; } else if (moveUp == false) { analogValue = 255; pulse = (analogValue * 19) / 10 + minPulse; // convert the analog value moveUp = true; } // to a range between minPulse // and maxPulse. int printValue = analogValue; Serial.println(printValue); startTime = now; } // pulse the servo again if rhe refresh time (20 ms) have passed: if (millis() - lastPulse >= refreshTime) { digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // save the time of the last pulse } } // this function blinks the an LED light as many times as requested void blinkLED(byte targetPin, int numBlinks, int blinkRate) { for (int i=0; i