I am making a Robot Lawnmower and it is very basic. I am using an UNO with the arduino Motor Shield. This is my first attempt at a sketch and using someone elses sketch as a guide (he used a different motor shield) along with a motor shield tutorial I found online, I came up with this.
/*
Mowbot software version 2f with random number turning
*/int bumpPin1 = 2;
int bumpPin2 = 3;
int val;
int randNumber;
void setup() {
// initialize the digital pins as an outputs.
// Pins to control motor shield
pinMode(12, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
}
void loop() {
val = analogRead(bumpPin1);
randNumber = random(600, 1500);
if (val == HIGH) { // if no objects detected on bump sensor right
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
else { // if objects detected on right sensor
digitalWrite(12, LOW); //Establishes backward direction of Channel B
digitalWrite(9, LOW); //Disengage the Brake for Channel B
analogWrite(3, 123); //Spins the motor on Channel B at half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 123); //Spins the motor on Channel B at half speed
delay(1500);
digitalWrite(13, LOW);// keep motor b running to turn
digitalWrite(8, LOW);
delay(randNumber);
analogWrite(11, 123); //Spins the motor on Channel B at half speed
}
val = analogRead(bumpPin2);
randNumber = random(600, 1500);
if (val == HIGH) { // if no objects detected on bump sensor right
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
else { // if objects detected on right sensor
digitalWrite(12, LOW); //Establishes backward direction of Channel B
digitalWrite(9, LOW); //Disengage the Brake for Channel B
analogWrite(3, 123); //Spins the motor on Channel B at half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 123); //Spins the motor on Channel B at half speed
delay(1500);
digitalWrite(13, LOW);// keep motor b running to turn
digitalWrite(8, LOW);
analogWrite(11, 123); //Spins the motor on Channel B at half speed
delay(randNumber);
}
}
Basically all I need it to do is go forward at full speed and if it bumps into something, back up, randomly turn and go forward again. I am using two micro switches for the bumpers. I have a 12VDC acid battery from an APC powering the Motor shield/motors, and a rechargable 9.6V (9v sized) battery powering the board. The Vin Connect pin I removed per suggestion from the Main site. The wiring goes from the battery, splits off to the blade motor and goes to the microswitches, then out of the switch to the motor shield, the other terminal on the micro switch will go to A2 and A3 but I haven't tried them connected yet. The ground connects to the ground for the blade motor, which connects to the battery.
Does any of this seem wrong? Any help would be appreciated.