Sunday, November 1, 2009

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// Motors
int irPin = 7; //
int irreading; // the analog reading from the analog resistor divider
int motor1speed = 10;
int motor1dir = 12;
int delayTime;
boolean state = false;

// Setup
void setup() {

Serial.begin(9600);

// Setup motors
pinMode(motor1speed, OUTPUT);
pinMode(motor1dir, OUTPUT);

pinMode(irPin, INPUT);
}

// Loop
void loop() {
irreading = analogRead(irPin);
Serial.println(irreading); // the raw analog reading

/* if (irreading > 135)
{
digitalWrite(motor1dir, LOW);
analogWrite(motor1speed, 200);
}

else if (irreading < 135)
{
digitalWrite(motor1dir, HIGH);
analogWrite(motor1speed, 200);
}*/

if(state)
{
motorForward();
} else {
motorBackward();
}


state = !state;
delayTime = random(4500, 10000);

delay(delayTime);


}

void motorForward()
{
digitalWrite(motor1dir, LOW);
analogWrite(motor1speed, 200);
}

void motorBackward()
{
digitalWrite(motor1dir, HIGH);
analogWrite(motor1speed, 500);
}

No comments:

Post a Comment