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

User avatar
By mrlightsman
#75470 I don't understand why this sketch isn't working. It seems simple and straightforward. Monitor RX pin for buttonpush (tied to ground, drive pin low) and toggle the GPIO_0 or GPIO_2 pin for a set length of time using a millis() timer function. It uses the modified version of avdweb_Switch library which includes "singleClick()" rather than "pushed()." Substituting the one bit in the code will make it compliant with the standard Switch library.

When I run the code, the TX LED glows dimly as if it is shorting out or trying to connect to wifi. I have completely erased the ESP (and tried a second ESP). The hardware is minimal for testing and has no wiring problems... it is fully tested.

Thanks.

Code: Select all#include <arduino.h>
#include <avdweb_Switch.h>


//Define pins
const byte GPIO_2 = 2; //To control Device 1
const byte GPIO_0 = 0; //To control Device 2
const byte RX = 3;  //To manually set the Esp into AP mode

//Define pin state for toggle
int GPIO_2State; //State of device 1
int GPIO_0State; //State of device 2


//Define Auto-off Timer variables
unsigned long startTime1; // timer for device 1
unsigned long startTime2; // timer for device 2
unsigned long currentTime;
unsigned long period;

//set up button debounce with Switch
Switch RXState = Switch(RX); //button to ground

void setup() {

    //Set up Auto-off Timers
  startTime1 = millis(); //save the start time for device 1
  startTime2 = millis(); //save the start time for device 2
 
  period = 600000;
}

void loop() {
  GPIO_2State = digitalRead(GPIO_2); //State of device 1 (on/Off)
  GPIO_0State = digitalRead(GPIO_0); //State of device 2 (on/Off)
 
 
  //What to do with button presses
  RXState.poll();

  if (RXState.doubleClick()) {
    digitalWrite(GPIO_0, !GPIO_0State);
    startTime2 = millis();
  }
     
  if (RXState.singleClick()) {
    digitalWrite(GPIO_2, !GPIO_2State);
    startTime1 = millis();
  }
 
  //Set timer
  currentTime = millis();

  //Auto-off timer device 1
  if (currentTime - startTime1 >= period) {
      digitalWrite(GPIO_2, HIGH);
      startTime1 = millis();
  }

  //Auto-off timer device 2
  if (currentTime - startTime2 >= period) {
      digitalWrite(GPIO_0, HIGH);
      startTime2 = millis();
  }         
}