Hi evereyone, i'm pretty new in arduino programming and i'm having a hard time writing my own code..i'm building a robotic vacumm cleaner and i basically need it to go straight until one of the limit switches is activated..if the right one is activated i want it to stop,turn and then continue going straight,the same goes for the other switch.Although the code seems correct to me i can't make it work..I'm in need of some help,thank you all in advance
here's the code:
#include <motorshield.h>
#include <Wire.h>
motorshield MotorShield;
int RelayModule=8;
int Lswitch=9;
int Rswitch=10;
int led=13;
#define GND 2
void setup ()
{
MotorShield.initialize();
Serial.begin(9600);
Serial.println("Dual DC Motor Shield test sketch");
pinMode(RelayModule,OUTPUT);
digitalWrite(RelayModule,HIGH);
pinMode(Lswitch, INPUT);
pinMode(led,OUTPUT);
//Setup Channel A
pinMode(6, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(10, OUTPUT); //Initiates Motor Channel A pin
pinMode(2, OUTPUT); //Initiates Brake Channel A pi
}
void loop() {
if ( (digitalRead (Rswitch) == LOW) and (digitalRead (Lswitch) == LOW) ){
digitalWrite(RelayModule,LOW);
digitalWrite(led,HIGH);
delay(500);
digitalWrite(6, 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(10,HIGH); //Establishes forward direction of Channel B
digitalWrite(2, LOW); //Disengage the Brake for Channel B
analogWrite(5, 255); //Spins the motor on Channel B at FULL speed
delay(500); }
if ( (digitalRead(Lswitch) == HIGH ) ) {
digitalWrite(RelayModule,LOW);
digitalWrite(led,LOW);
delay(500);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(2, HIGH); //Engage the Brake for Channel B
delay(500);
digitalWrite(6, LOW); //Establishes backward 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(10,LOW); //Establishes backward direction of Channel B
digitalWrite(2, LOW); //Disengage the Brake for Channel B
analogWrite(5, 255); //Spins the motor on Channel B at FULL speed
delay(1000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(2, HIGH); //Engage the Brake for Channel B
delay(500);
digitalWrite(6,HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3,0); //Spins the motor on Channel A at ZERO speed
digitalWrite(10,HIGH); //Establishes forward direction of Channel B
digitalWrite(2, LOW); //Disengage the Brake for Channel B
analogWrite(5,255); //Spins the motor on Channel B at FULL speed
Serial.println("if process ended - going to start loop again");
delay(500); }
if ( (digitalRead(Rswitch) == HIGH) ) {
Serial.println("switch just pressed!"); //
digitalWrite(RelayModule,LOW);
digitalWrite(led,LOW);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(2, HIGH); //Engage the Brake for Channel B
delay(500);
digitalWrite(6, LOW); //Establishes backward 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(10,LOW); //Establishes backward direction of Channel B
digitalWrite(2, LOW); //Disengage the Brake for Channel B
analogWrite(5, 255); //Spins the motor on Channel B at FULL speed
delay(1000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(2, HIGH); //Engage the Brake for Channel B
delay(500);
digitalWrite(6,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(10,HIGH); //Establishes forward direction of Channel B
digitalWrite(2, LOW); //Disengage the Brake for Channel B
analogWrite(5,0); //Spins the motor on Channel B at ZERO speed
Serial.println("if process ended - going to start loop again");
delay(500);
}
}