I am having trouble running multiple commands together in one loop, first I am new to coding and I am trying my best to learn how to make this code work. I want to have multiple operations happen at once on one arduino when commanded by an arduino with RF, I can make the arduino when commanded perform the single operation perfectly when it's running by itself but when I send another command to the arduino the two operations don't want to work together correctly. I have to turn all the codes off and start again to have what I want to perform correctly. I tried making it with state machine but I think what I am doing is wrong, can someone please help me out.
#include <VirtualWire.h>
#include <SM.h>
int sensor1Pin =A5;
int sensor2Pin = 7;
int ledonePin = 12;
int ledtwoPin = 13;
int sensor1powerPin = 9;
int sensor2powerPin = 8;
SM sensor1a(sensor1on);
SM sensor1b(sensor1off);
SM sensor2a(sensor2on);
SM sensor2b(sensor2off);
void setup()
{
vw_setup(2000);
vw_set_rx_pin(11);
vw_rx_start();
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
pinMode(sensor1powerPin, OUTPUT);
pinMode(ledonePin, OUTPUT);
pinMode(sensor2powerPin, OUTPUT);
pinMode(ledtwoPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
Serial.print((char)buf[0], HEX);
}
if((char)buf[0] == 'a')
{
EXEC(sensor1a);
}
if((char)buf[0] == 'b')
{
EXEC(sensor1b);
}
if((char)buf[0] == 'c')
{
EXEC(sensor2a);
}
if((char)buf[0] == 'd')
{
EXEC(sensor2b);
}
}
State sensor1on()
{
digitalWrite(sensor1powerPin, HIGH);
int val = analogRead(sensor1Pin);
if (val > 700) digitalWrite(ledonePin, HIGH);
else if (val < 700) digitalWrite(ledonePin, LOW);
}
State sensor1off()
{
digitalWrite(sensor1powerPin,LOW);
digitalWrite(ledonePin,LOW);
}
State sensor2on()
{
digitalWrite(sensor2powerPin, HIGH);
int val = digitalRead(sensor2Pin);
if (val == HIGH)
{
digitalWrite(ledtwoPin, HIGH);
}
else if (val == LOW)
{
digitalWrite(ledtwoPin, LOW);
}
}
State sensor2off()
{
digitalWrite(sensor2powerPin, LOW);
digitalWrite(ledtwoPin,LOW);
}
Yes I did it with switch cases at first and I still had the same problem, so I decided to try state machine and the problem still happened. Here is the case statement code I tried.
#include <VirtualWire.h>
int sensor1Pin =A5;
int sensor2Pin = 7;
int ledonePin = 2;
int ledtwoPin = 4;
int sensor1powerPin=3;
int sensor2powerPin=8;
void setup()
{
vw_setup(2000);
vw_set_rx_pin(11);
vw_rx_start();
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
pinMode(sensor1powerPin, OUTPUT);
pinMode(ledonePin, OUTPUT);
pinMode(sensor2powerPin, OUTPUT);
pinMode(ledtwoPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
Serial.print((char)buf[0], HEX);
}
{
char inChar = (char)buf[0];
switch(inChar)
{
case 'a':
{
digitalWrite(sensor1powerPin, HIGH);
int val = analogRead(sensor1Pin);
if (val > 700)
digitalWrite(ledonePin, HIGH);
else if (val < 700)
digitalWrite(ledonePin, LOW);
}
break;
case 'b':
digitalWrite(sensor1powerPin,LOW);
digitalWrite(ledonePin,LOW);
break;
case 'c':
{
digitalWrite(sensor2powerPin,HIGH);
int val = digitalRead(sensor2Pin);
if (val == HIGH)
digitalWrite(ledtwoPin, HIGH);
else if (val == LOW)
digitalWrite(ledtwoPin, LOW);
}
break;
case 'd':
{
digitalWrite(sensor2powerPin,LOW);
digitalWrite(sensor2Pin,LOW);
}
}
}
}
The code you have written seems to be intended to do one of several actions depending on what char value is received.
From you original post it appears that you want something different to happen, but I can't figure out what that is.
If you want action A and action B to happen at the same time you need a very different approach - something like this pseudo code
loop() {
getInstruction();
actionA();
actionB();
}
getInstruction() {
get stuff from wherever;
set the values of variables to record what was received
e.g. if an A is received set varStateA = true
}
actionA() {
if (varStateA == true) {
do this action
}
}
Yes, I want when this char is received perform this action and if this char is also received perform this action as well. I want to be able to have multiple sensors and actions to happen simultaneously if different char are sent out. To be able to turn them on and off when char is sent. They work the way I want when one is sent but when another is sent they interfere with each other.
Okay, thank I will try to use what you wrote in my code and see if this works. I am so stressed out about making this work it is already turning into a headache with probably a simple fix.