one motor controls steering and one for forward reverse my current code only lets me do one thing at a time f,b,left,right i of course would like to move and steer at the same time ill post my tx and rx side code
int SW1 = 8; //forward
int SW2 = 9; //backward
int SW3 = 3; //left
int SW4 = 2; //right
//int SW5 = ; //lights
void setup()
{
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
pinMode(SW4, INPUT_PULLUP);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
Serial.begin(9600);
}
void loop(void)
{
unsigned char i=0;
//backward
if ( digitalRead(SW1) == HIGH)
{
(i = 1);
Serial.println("Backward");
}
//forward
else if ( digitalRead(SW2) == HIGH)
{
(i = 2);
Serial.println("Forward");
}
//left
else if ( digitalRead(SW3) == HIGH)
{
(i = 3);
Serial.println("Left");
}
//right
else if ( digitalRead(SW4) == HIGH)
{
(i = 4);
Serial.println("Right");
}
else
{
(i = 5);
}
send_data(i);
Serial.print("i=");
Serial.println(i,DEC);
delay(500);
send_data(0);
}
//====================================
void send_data(unsigned char data)
{
digitalWrite(4,(data&0x01));
digitalWrite(5,(data&0x02));
digitalWrite(6,(data&0x04));
digitalWrite(7,(data&0x08));
}
You need to send more than the 4 cardinal values. If two switches are pressed,, you need to send a value that indicates that. Instead of the 1, 2, 3, and 4 you have now, those values should be 1, 2, 4, and 8. Look at the binary pattern for those numbers to see why.
Then, when two switches are pressed, add the values for the two switches. For instance if the 2nd and 3rd switch are pressed, you'd send 6 (2 + 4), to tell the receiver to turn left while moving forward. If the 1st and 4th switches are pressed, you'd send 9 (1 + 8), to turn right while backing up.
(Lose) (the) (useless) (parentheses) (around) (everything). Learn to use them only when needed.
Interrupt service routines are NOT the place to be doing Serial.print().
Variables shared between an ISR and non-interrupt functions, like loop(), need to be declared volatile.
i did update y code and it compiles good but is there an easier way then all the if statments?
Yes. The reason for using 1, 2, 4, and 8 as the values associated with the switches is so that you can simply add the value corresponding to a switch, if the switch is pressed.
int i = 0;
if(digitalRead(SW1) == HIGH)
i += 1;
if(digitalRead(SW2) == HIGH)
i += 2;
if(digitalRead(SW3) == HIGH)
i += 4;
if(digitalRead(SW4) == HIGH)
i += 8;
// Now send i
Look at what this code sets i to if only one switch is pressed. Look at what it sets i to if two (or more) switches are pressed.
oh i see so it just adds it together if two are pressed so say SW1 and SW3 are pressed i would only need to tell my RX side what to do if data value is 5?
Not understanding the subtracting part what number does it subtract from?
The number you sent it. If you send it 5, you can see that it is not greater than or equal to 8, so the 8 action is not needed. It is greater than or equal to 4, so the 4 action is needed. Note that, and subtract 4, leaving 1. That value is not greater than or equal to 2, so the 2 action is not needed. The value is greater than or equal to 1, so the 1 action is needed. Note that, and subtract 1.
Now, perform the actions that you noted needed to be done - 4 and 1.