Let's Make Robots!

motor command via Serial prob.

i need to command motors to start and stop via the serial line.   but i upload the code and they both just come on?  wont go off or enything.  they are small so for thesting i jush atached them to the pins and ground heres the code:

 

 

 

 

int message = 1;
int M1P =  5;
int M2P =  6;
int M1 = 0;
int M2 = 0;

void setup()
{
  pinMode(M1P, OUTPUT); 
  pinMode(M2P, OUTPUT);
  Serial.begin(9600);  
}


void loop()                     
{
  {
  if (Serial.available() > 0)
  message = Serial.read();
}{
  if (message == 'R');
  digitalWrite(M1P, HIGH); 
}{
  if (message == 'r');
  digitalWrite(M1P, LOW);
}{
  if (message == 'L');
  digitalWrite(M2P, HIGH);
}{
  if (message == 'l');
  digitalWrite(M2P, LOW);  

}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I don't know why I said that earlier...
A few suggestions - your scope operators { & } are not necessary 
Also if you are going to test message as a char you should define it as a char

 

 

char message = 1;
int M1P =  5;
int M2P =  6;
int M1 = 0;
int M2 = 0;

void setup()
{
  pinMode(M1P, OUTPUT); 
  pinMode(M2P, OUTPUT);
  Serial.begin(9600);  
}


void loop()                     
{
	if (Serial.available() > 0)
	{
       	  message = Serial.read();
          
	  if (message == 'R');
            digitalWrite(M1P, HIGH); 
 	  
	  if (message == 'r');
   	    digitalWrite(M1P, LOW);
          
	  if (message == 'L');
  	    digitalWrite(M2P, HIGH);
 
	  if (message == 'l');
  	    digitalWrite(M2P, LOW);  
	}
} 


http://www.arduino.cc/en/Serial/Read

 

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

 

message is a byte but you are comparing it with a ascii char 'R'

 

instead of 'R' ... try comparing it with 82   82 is ascii 'R'

 

http://www.ascii-code.com/ 

 


 

Your first line should be something like:

char message;

if you can get away with declaring a variable without also initializing it. Otherwise,

char message='l'; or char message='r';

should work. I honestly don't know C well enough to say for sure.

You defined message as an integer but seem to be using it as a char or string. My guess is that's why they just stay on. It's an int defined as one and the character changes can't affect it after. Try changing that.

If that doesn't work try using softSerial library to setup a second COM port. Send commands via that port and use the default port as a serial monitor.

Try that and write here if you are still stuck.

i dont know how to do that can you explane?