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

User avatar
By redtom
#86829 Firstly to btidey: I got it working perfectly today with your code. Thanks a million. I really appreciate the trouble you went to.

Rudy, thank you for your input also but I'm not quite sure what you meant. Do you mean that I should look at the ADC values in the code and possibly alter them?

Regards to both of you

Dermot
User avatar
By btidey
#86839 The point on the ESP8266 ADC is that although it is nominally a 10 bit converter, it is in practice quite noisy in its conversions behaving more like a 6 or 7 bit converter. So even with a steady voltage input you would see the digital value jumping around by maybe +/- 15. It is OK for monitoring failry crude values like a battery voltage. If you were using it in your application to just distinguish between two values with a big difference then it would be fine. It is also the reason why I put in 2 different thresholds in the original code.

If the sensor you want to control your switch can give a digital low / high signal then it would be better to use one of the GPIO pins instead. This would also make the code simpler. Try to figure that out for yourself but post back if you need any further pointers.
User avatar
By redtom
#87084
btidey wrote: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.


Hi btidey,
Thanks again for the help you gave before. What you suggested worked perfectly. I'm now trying to add an extra feature to my project.
I want to add an extra condition to the ON code being sent. I want to have the code sent only if the adcVal is above the threshold AND GPIO14 is high.
I have tried the code below which is the same as yours but just adds a further condition to the if statement. It compiles fine but doesn't work on the hardware (ESP8266 12-E Lolin.

It won't turn the switch on when the adcVal is high and GPIO14 is high.It's driving me crazy :D Any help would be much appreciated.
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#define ON_THRESHOLD 500
#define OFF_THRESHOLD 400
int adcVal;
int state = -1;
const int D1Pin = 14; //pin D5
const int D2Pin = 12; //pin D6
const int D3Pin = 13; //pin D7

void setup() {

mySwitch.setRepeatTransmit(15); // Optional set number of transmission repetitions.
mySwitch.setProtocol(1); // Optional set protocol (default is 1, will work for most outlets)
mySwitch.enableTransmit(1); // Transmitter is connected to Arduino Pin #10
mySwitch.setPulseLength(314); // Optional set pulse length.
pinMode(D1Pin, INPUT);

}

void loop() {
adcVal = analogRead(A0);

if((adcVal >= ON_THRESHOLD) && digitalRead(D1Pin != LOW)){
setSwitchOn();
} else {
setSwitchOff();
}
delay(10);
}

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

void setSwitchOff() {
if(state != 0) {
mySwitch.send("100111101111101000001110");
state = 0;
}
}
User avatar
By redtom
#87101 DOH!!

I see my mistake now. I should have had a variable to store the state of each pin, read the pin and then see if that value was high or low.