So am having an issue getting my Robot code to work right. Its run through Windows 10 via Serial Com.
The Line BIGSERVOSW works when the button is pressed (and held), But it will not activate when Serial COM 5 is sent.
Same thing with the EXTRASERVO at the bottom.
I can not figure out why it work work with serial communication.
I also realize the very last item is not correct i am working on that yet/need to figure that out as well
PROGRAM:
#include <Servo.h>
#define BIGSERVOSW 5
#define EXTRASERVOSW 4
#define ROCK1 2
#define ROCK2 3
#define ROCKETFIRE1 12
#define ROCKETFIRE2 11
#define LED 10
Servo DoorServo;
Servo servo2;
Servo servo3;
char c;
String cmd = "";
void setup() {
// put your setup code here, to run once:
pinMode(BIGSERVOSW, INPUT);
pinMode(EXTRASERVOSW, INPUT);
pinMode(ROCK1, INPUT);
pinMode(ROCK2, INPUT);
pinMode(ROCKETFIRE1, OUTPUT);
pinMode(ROCKETFIRE2, OUTPUT);
pinMode(LED,OUTPUT);
DoorServo.attach(7);
servo2.attach(6);
servo3.attach(9);
// LETS PULL UP THE INPUTS
digitalWrite(BIGSERVOSW, HIGH);
digitalWrite(EXTRASERVOSW, HIGH);
digitalWrite(ROCK1, HIGH);
digitalWrite(ROCK2, HIGH);
DoorServo.write(0);
servo2.write(0);
servo3.write(0);
Serial.begin(9600);
digitalWrite(ROCKETFIRE1, LOW); //get back to default
digitalWrite(ROCKETFIRE2, LOW); //get back to default
digitalWrite(LED, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
getSerial();
// Check for bigservo switch
if ( digitalRead(BIGSERVOSW) == LOW || cmd == "5")
{
/* according to "Then a Separate servo with its own button when pressed
moves a Large servo 120 degrees then returns when you let of the button" */
servo2.write(120);
while( servo2.read() != 120) //do nothing till servo come to 120
{}
Serial.println(" BIGSERVO 120deg arrived " );
servo2.write(0); //bring servo back to 0
delay(500); //for debounce
}
else if ( digitalRead(ROCK1) == LOW || cmd == "1")
{
Serial.println(" ROCK1 pressed" );
DoorServo.write(95); // open the door for 95 deg
while( DoorServo.read() != 95) //do nothing till servo come to 95
{}
delay(1000); // dealy for 1 sec
digitalWrite(ROCKETFIRE1, HIGH); //fire the rocket 1
delay(2000); //delay for 2 sec
digitalWrite(ROCKETFIRE1, LOW); //get back to default
Serial.println(" Rocket low" );
delay(1000);
DoorServo.write(0); // close the door again 0deg
while( DoorServo.read() != 0) //do nothing till servo come to 0
{}
Serial.println(" Door closed" );
delay(500); //for debounce
cmd = "";
}
else if ( digitalRead(ROCK2) == LOW || cmd == "2" )
{
DoorServo.write(95); // open the door for 95 deg
Serial.println(" Door opened" );
while( DoorServo.read() != 95) //do nothing till servo come to 95
{}
delay(1000); // dealy for 1 sec
digitalWrite(ROCKETFIRE2, HIGH); //fire the rocket 1
delay(2000); //delay for 2 sec
digitalWrite(ROCKETFIRE2, LOW); //get back to default
delay(1000);
DoorServo.write(0); // close the door again 0deg
while( DoorServo.read() != 0) //do nothing till servo come to 0
{}
Serial.println(" Door closed" );
delay(500); //for debounce
cmd = "";
}
else if ( digitalRead(EXTRASERVOSW) == LOW || cmd == "3")
{
Serial.println(" EXTRASERVO Pressed" );
/* according to "Then a Separate servo with its own button when pressed
moves a Large servo 120 degrees then returns when you let of the button" */
servo3.write(120);
while( servo3.read() != 120) //do nothing till servo come to 120
{}
Serial.println(" EXTRASERVO 120deg arrived " );
while ( digitalRead(EXTRASERVOSW) == LOW || cmd == "EXTRASERVOSW") //do nothing till switch is being pressed.
{getSerial();}
Serial.println(" switch released" );
servo3.write(0); //bring servo back to 0
delay(500); //for debounce
}
if ( digitalRead(LED) == HIGH || cmd == "6" )
{
digitalWrite(LED, HIGH);
}
if ( digitalRead(LED) == LOW || cmd == "8" )
{
digitalWrite(LED, LOW);
}
else if ( digitalRead (ROCK1) == LOW ||cmd == "7" )
{
DoorServo.write(95); // open the door for 95 deg
Serial.println(" Door opened" );
while( DoorServo.read() != 95) //do nothing till servo come to 95
{}
delay(1000); // dealy for 1 sec
digitalWrite(ROCKETFIRE1, HIGH); //fire the rocket 1
delay(1000); // dealy for 1 sec
digitalWrite(ROCKETFIRE2, HIGH); //fire the rocket 2
delay(2500); //delay for 2 sec
digitalWrite(ROCKETFIRE1, LOW);
digitalWrite(ROCKETFIRE2, LOW); // get back to default
delay(2000);
DoorServo.write(0); // close the door again 0deg
while( DoorServo.read() != 0) //do nothing till servo come to 0
{}
Serial.println(" Door closed" );
delay(500); //for debounce
cmd = "";
}
}
void getSerial(){
cmd = "";
if(Serial.available()>0){
cmd = "";
c = Serial.read();
cmd = c;
while(Serial.available()>0){
cmd.concat((char)Serial.read());
}
}
}