Hi
I have a phone connected to an rpi which then sends commands on serial to my arduino which have a relay board that controls two blind motors, 4 push buttons(in case you dont want to use the phone) and 4 status led.
The blind motor are connected to NC for motor up and NO for motor down on the relay board and another relay cuts the power to the relay to turn the motor off.
The format I would like to use from the RPI is:
node-id;child-sensor-id;message-type;ack;sub-type;payload\n
where
My arduino node number is 2, (Never changing)
My sensor number is 0 for the relay connected to motorPinOnOff
My sensor number is 1 for the relay connected to motorPinSprit
My sensor number is 2 for the relay connected to motorPinVin
My message-type is 0 for incoming message
ack is 0
subtype= 3 for onOff relay and 5 for motors
and the payload is
0 for Motor Up.
1 for Motor Down.
0 for motor Off
1 for Motor On
So the message would look like this for enabling motors
2;0;0;0;3;1\n
and this for turning motor for wine up
2;2;0;0;5;0\n
But my question is how can I get this implemented in my code below, currently i am just sending a singel integer, and can i somehow reduce number of if sentences? And what should i set my delays to?
// constants won't change. They're used here to
// set pin numbers:
const int buttonPinSpritOpp = 9; // the number of the pushbutton pin
const int buttonPinSpritNed = 12; // the number of the pushbutton pin
const int buttonPinVinOpp = 10; // the number of the pushbutton pin
const int buttonPinVinNed = 11; // the number of the pushbutton pin
const int ledPinSpritOpp = 7; // the number of the pushbutton pin
const int ledPinSpritNed = 6; // the number of the pushbutton pin
const int ledPinVinOpp = 4; // the number of the pushbutton pin
const int ledPinVinNed = 3; // the number of the pushbutton pin
const int motorPinVin = A2; // the number of the motor pin
const int motorPinOnOff = A3; // the number of pin to turn motors completly off
const int motorPinSprit = A0; // the number of the motor pin
int intCode =-1;
long startTime = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(motorPinSprit, OUTPUT);
pinMode(motorPinVin, OUTPUT);
pinMode(motorPinOnOff, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinSpritOpp, INPUT_PULLUP);
pinMode(buttonPinSpritNed, INPUT_PULLUP);
pinMode(buttonPinVinOpp, INPUT_PULLUP);
pinMode(buttonPinVinNed, INPUT_PULLUP);
digitalWrite(motorPinSprit,HIGH);
digitalWrite(motorPinVin,HIGH);
digitalWrite(motorPinOnOff, HIGH);
Serial.begin(9600);
Serial.println("starting now...");
}
void loop() {
if ( Serial.available()) // Check to see if at least one character is available
{
startTime = millis();
char ch = Serial.read();
if( isDigit(ch) ) // is this an ascii digit between 0 and 9?
{
intCode = (ch - '0'); // ASCII value converted to numeric value
}
}
if (millis() -startTime> 2000 ){
intCode=-1;
}
if ((digitalRead(buttonPinSpritOpp) == HIGH && digitalRead(buttonPinSpritNed) == LOW) || intCode==1) {
digitalWrite(motorPinSprit,HIGH);
digitalWrite(motorPinOnOff,LOW);
digitalWrite(ledPinSpritOpp,HIGH);
digitalWrite(ledPinSpritNed,LOW);
Serial.println("Sprit opp");
}
else if ((digitalRead(buttonPinSpritOpp) == LOW && digitalRead(buttonPinSpritNed) == HIGH)|| intCode==2) {
digitalWrite(motorPinSprit,LOW);
digitalWrite(motorPinOnOff,LOW);
digitalWrite(ledPinSpritNed,HIGH);
digitalWrite(ledPinSpritOpp,LOW);
Serial.println("Sprit ned");
}
else{
digitalWrite(motorPinSprit,HIGH);
digitalWrite(motorPinOnOff,HIGH);
digitalWrite(ledPinSpritOpp,LOW);
digitalWrite(ledPinSpritNed,LOW);
}
if ((digitalRead(buttonPinVinOpp) == HIGH && digitalRead(buttonPinVinNed) == LOW) || intCode==3) {
digitalWrite(motorPinVin,HIGH);
digitalWrite(motorPinOnOff,LOW);
digitalWrite(ledPinSpritOpp,HIGH);
digitalWrite(ledPinSpritNed,LOW);
Serial.println("Vin opp");
}
else if ((digitalRead(buttonPinVinOpp) == LOW && digitalRead(buttonPinVinNed) == HIGH)|| intCode==4) {
digitalWrite(motorPinVin,LOW);
digitalWrite(motorPinOnOff,LOW);
digitalWrite(ledPinSpritNed,HIGH);
digitalWrite(ledPinSpritOpp,LOW);
Serial.println("Vin ned");
}
else if (intCode==0){
digitalWrite(motorPinOnOff,HIGH);
digitalWrite(ledPinSpritOpp,LOW);
digitalWrite(ledPinSpritNed,LOW);
Serial.println("Shutting off");
}
else{
digitalWrite(motorPinVin,HIGH);
digitalWrite(motorPinOnOff,HIGH);
digitalWrite(ledPinSpritOpp,LOW);
digitalWrite(ledPinSpritNed,LOW);
}
delay(400);
}