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

User avatar
By redtom
#86808 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 command 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 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:
'#picaxe 14M2

readadc C.4,b0 'read value on ADC pin C.4 and put that value into variable b0
if b0>=120 and b1=0 then goto plugON 'b1 is variable to store the state of the plug, 1 for ON and 0 for OFF
if b0<120 and b1=1 then goto plugOFF

goto Start

plugON:
mySwitch.send("100111101111101000001111");
b1=1
goto Start

plugOFF:
mySwitch.send("100111101111101000001110");
b1=0
goto Start

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

Regards

Dermot
User avatar
By btidey
#86818 Here is one suggestion for the loop code

Code: Select all#define ON_THRESHOLD 500
#define OFF_THRESHOLD 400
int adcVal;
int state = -1;

void loop() {
   adcVal = analogRead(A0);
   if(adcVal >= ON_THRESHOLD) {
      setSwitchOn();
   } else if(adcVal <= OFF_THRESHOLD) {
      setSwitchOff();
   }
   delay(10);
}

void setSwitchOn() {
   if(state != 1) {
      mySwitch.send("100111101111101000001111");
      state = 1;
   }
}

void setSwitchOff() {
   if(state != 0) {
      mySwitch.send("100111101111101000001110");
      state = 0;
   }
}


ADC range is 0 - 1024 . I suggest having different thresholds for on and off otherwise if the value was near the middle then it could toggle on and off due to adc noise. These are put into #defines which are just a more readable way of setting their values. state was initialised to -1 so that the first call to switchOn or switchOff is always actioned to set the state accordingly.
User avatar
By redtom
#86820 Thank you very much. I'm going to learn a lot from your code. I think I might use a digital input after I get your code working. It may be simpler. Thanks again

Regards

Dermot