Hello All,
I am trying to write code for a servo that will move in either CW or CCW motion from a random point and increase by +1 or decrease -1 from that point until it reaches either 0 or 180. Here is the code I have so far.
//Written painfully by Spaceman
#include <Servo.h>
Servo servo01;
int pos = 0;
void setup ()
{
Serial.begin(9600);
servo01.attach(9);
}
void loop()
{
int OddEven = random(1, 3);
int randNum = random(0, 180);
void loop();
Serial.print("OddEven");
Serial.println(OddEven); // so I can see what value I get
delay(250); //time to read
Serial.print("randNum");
Serial.println(randNum); // so I can see what value I get
delay(250); //time to read
if (OddEven = 1)
{
for(pos = randNum; pos < 180; pos +=1)
servo01.write(pos);
Serial.print("Position");
Serial.println(pos); // so I can see what value I get
delay(250); //time to read
}
else if (OddEven = 2)
{
for(pos = randNum; pos < 180; pos -=1)
servo01.write(pos);
Serial.print("Position2");
Serial.println(pos); // so I can see what value I get
delay(250); //time to read
}
delay(500); //time to read
}
When I run the code, I can see that the direction (OddEven) random number is working and the random number to start the incremental positioning is working. It is the incremental/decremental 'for' statement that doesn't seem to work. It always stays at 180. Also, I never get to the 'else if' part of the loop.
I've been lurking in here for a while and thought I could figure this out on my own. I don't have much experience writing code and clearly I need help.
Any thought or hints would be appreciated.
Also, a little shout-out to those that run this place.... nice work!