Good Morning (here anyhow)!
I don't know why I am having so much difficulty with this :-[
I don't want this done for me
I will include code that I hoped will turn a servo 0,90,180 degrees and back to 0 again. The servo hosts a ping ultrasound sensor that I query for distance info. All works splendidly when I use the irksome delay command. However when I try to implement millis timing I have problems.
Perhaps if someone could look at this they would see where my logic fails. I am trying to follow advice I've been given but I just don't seem to understand how to implement it.
In the example, the code for the 0 degree position is what I would like to do (millis timing?). The next sections of code (for 90,180) have been left the way I originally wrote it (meaning copied and pasted) to help explain what I am trying to accomplish.
#include <Servo.h>
#include <Ping.h>
Ping ping = Ping(8,74,19);
Servo myservo; // create servo object to control a servo
unsigned long currentTime = 0;
unsigned long previousTime = 0;
int pos = (0); // variable to store the servo position
int outputPin = 12;
int outputPin2 = 13;
int outputPin3 = 11;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(outputPin, OUTPUT);
pinMode(outputPin2, OUTPUT);
pinMode(outputPin3, OUTPUT);
}
void loop()
{
currentTime = millis(); //get current time?
unsigned long millisPassed = currentTime-previousTime;
//declare a variable = current time - previous
ping.fire();
Serial.print("Microseconds: ");
Serial.print(ping.microseconds());
Serial.print(" | Inches ");
Serial.print(ping.inches());
Serial.println();
myservo.write(0); //turn servo to 0 degrees
Serial.print(0); //print 0
Serial.println(" "); //print space
if (millisPassed >= previousTime +1500)
//if time passed is greater than or equal to
//previous time plus 1500 milsec then...
digitalWrite(outputPin2, HIGH); // turn on light at pin2
if (millisPassed >= previousTime +35)
//if time passed is greater than or equal to
//previous time plus 35 milsec then...
digitalWrite(outputPin2, LOW); // turn off light pin2
myservo.write(90);
Serial.print(90);
Serial.println(" "); //the next examples work
digitalWrite(outputPin3, HIGH); //as written
delay(1000);
digitalWrite(outputPin3, LOW);
delay(35);
myservo.write(180);
Serial.print(180);
Serial.println(" ");
digitalWrite(outputPin, HIGH);
delay(1000);
digitalWrite(outputPin, LOW);
delay(35);
myservo.write(90);
Serial.print(90);
Serial.println(" ");
digitalWrite(outputPin3, HIGH);
delay(1000);
digitalWrite(outputPin3, LOW);
delay(35);
}