/************************************************************* Created By. Firaz.A.Peerjade *************************************************************/ int pinfrontLights = 4; int pinbackLights = 7; int pinhorn =5; int state; int flag=0; //makes sure that the serial only prints once the state int stateStop=0; void setup() { //Setup Channel A pinMode(12, OUTPUT); //Initiates Motor Channel A pin pinMode(9, OUTPUT); //Initiates Brake Channel A pin //Setup Channel B pinMode(13, OUTPUT); //Initiates Motor Channel A pin pinMode(8, OUTPUT); //Initiates Brake Channel A pin pinMode(pinhorn, OUTPUT); pinMode(pinfrontLights , OUTPUT); pinMode(pinbackLights , OUTPUT); Serial.begin(9600); } void loop() { // if the state is 'V' turn ON horn if (state == 'V') { digitalWrite(pinhorn, HIGH); } // if the state is 'v' turn OFF horn if (state == 'v') { digitalWrite(pinhorn, LOW); } // if the state is 'U' turn ON front lights if (state == 'U') { digitalWrite(pinfrontLights, HIGH); } // if the state is 'u' turn OFF front lights if (state == 'u') { digitalWrite(pinfrontLights, LOW); } // if the state is 'W' turn ON back lights if (state == 'W') { digitalWrite(pinbackLights, HIGH); } // if the state is 'w' turn OFF back lights if (state == 'w') { digitalWrite(pinbackLights, LOW); } // if the state is 'S' the DC motor will turn off if (state == 'S') { //Motor A forward @ full speed digitalWrite(12, HIGH); digitalWrite(9, HIGH); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, HIGH); digitalWrite(8, HIGH); analogWrite(11,255); if(flag == 0){ Serial.println("Motor: off"); flag=1; } } //if some date is sent, reads it and saves in state if(Serial.available() > 0){ state = Serial.read(); flag=0; } // if the state is 'F' the DC motor will go forward if (state == 'F') { //Motor A forward @ full speed digitalWrite(12, HIGH); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, HIGH); digitalWrite(8, LOW); analogWrite(11,255); if(flag == 0){ Serial.println("Go Forward!"); flag=1; } } // if the state is 'B' the motor will will go backward else if (state == 'B') { //Motor A forward @ full speed digitalWrite(12, LOW); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, LOW); digitalWrite(8, LOW); analogWrite(11,255); if(flag == 0){ Serial.println("Backward"); flag=1; } delay(150); state=3; stateStop=1; } // if the state is 'R' the motor will turn Right else if (state == 'R') { //Motor A forward @ full speed digitalWrite(12, HIGH); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, LOW); digitalWrite(8, LOW); analogWrite(11,255); if(flag == 0){ Serial.println("turn Right"); flag=1; } stateStop=0; } // if the state is 'L' the motor will turn Left else if (state == 'L') { //Motor A forward @ full speed digitalWrite(12, LOW); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13,HIGH); digitalWrite(8, LOW); analogWrite(11,255); if(flag == 0){ Serial.println("Turn Left"); flag=1; } delay(150); state=3; stateStop=1; } // if the state is 'I' turn right forward if (state == 'I') { //Motor A forward @ full speed digitalWrite(12,HIGH); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, LOW); digitalWrite(8, LOW); analogWrite(11,255); if(flag == 0){ Serial.println("right forward!"); flag=1; } } // if the state is 'G' turn left forward if (state == 'G') { //Motor A forward @ full speed digitalWrite(12,LOW); digitalWrite(9, LOW); analogWrite(3, 255); //Motor B forward @ full speed digitalWrite(13, HIGH); digitalWrite(8, LOW); analogWrite(11,255); } }