So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By redtom
#86803 Hi. I’m new to the arduino and ESP8266 but have some limited experience coding picaxe. I’ve been working with the RCSwitch.h library which deals with mimicing RF remote controls. I have it installed as part of a sketch used to send RF signals to a remote RF socket. It basically mimics the remote control.

I find the C/C++ environment new and a bit challenging. Basically I need some help in declaring variables and running commands based on if statements. I want to run a commands like

mySwitch.send("100111101111101000001111");

based on the state (high or low) of an input whether analog or digital. If the input to the digital or analog pin is high I want the sketch to send a the following command:

mySwitch.send("100111101111101000001111");

and if the state of the input is low I want it to send this command:

mySwitch.send("100111101111101000001110");


This is the sketch which I use to turn ON and OFF the socket for 1 second intervals. It works on my ESP8266 and arduino uno. It’s just a proof of concept and turns the plug on and off no problem at 1 second intervals.


/*
Based on the SendDemo example from the RC Switch library
https://github.com/sui77/rc-switch/
*/

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
Serial.begin(9600);

// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);

// Optional set pulse length.
mySwitch.setPulseLength(314);

// Optional set protocol (default is 1, will work for most outlets)
mySwitch.setProtocol(1);


}

void loop() {
// Binary code - button 3
mySwitch.send("100111101111101000001111"); //turns ON switch
delay(1000);
mySwitch.send("100111101111101000001110"); //turns OFF switch
delay(1000);


}

This works fine and turns the plug ON and OFF once a second. What I want to do is to set a condition in the loop part of the code to only turn the switch on i.e. execute

mySwitch.send("100111101111101000001111");

if an input pin goes high (either analog or digital). As soon as the input goes low then the plug would be turned off by sending

mySwitch.send("100111101111101000001110");

I think I would need some sort of variable to store the state of the plug, statevar say, maybe 0 for plug off and 1 for plug on. I suppose this variable would have to be declared in the setup part of the sketch?
If I were doing this in picaxe basic it would look some thing like this;

start:
if analogpin>240 and statevar=0 then mySwitch.send("100111101111101000001111"); and statevar=1

if analogpin<=240 and statevar=1 then mySwitch.send("100111101111101000001110"); and statevar=0

goto start

I would appreciate any help you could give. Thank you in advance.

Regards

Dermot
User avatar
By AcmeUK
#86862
If I were doing this in picaxe basic it would look some thing like this;

start:
if analogpin>240 and statevar=0 then mySwitch.send("100111101111101000001111"); and statevar=1

if analogpin<=240 and statevar=1 then mySwitch.send("100111101111101000001110"); and statevar=0

goto start

Search Google for Arduino If.