-->
Page 1 of 2

What have I done Wrong? Simple Switch code...

PostPosted: Wed Apr 18, 2018 6:15 pm
by mrlightsman
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();
  }         
}

Re: What have I done Wrong? Simple Switch code...

PostPosted: Wed Apr 18, 2018 9:11 pm
by rudy
I don't know how the library you are using works and I wouldn't waste my time looking at it considering your obvious mistakes.

What determines how a port pin operates?

Re: What have I done Wrong? Simple Switch code...

PostPosted: Thu Apr 19, 2018 6:51 am
by mrlightsman
While the response was a bit snarky, I do appreciate the feedback as it made me realize... "Duh, I forgot the pinmode commands!" So, thank you.

Re: What have I done Wrong? Simple Switch code...

PostPosted: Thu Apr 19, 2018 11:27 am
by kaxx1975
Lol, you took that a lot better than I did. Guess he fits his name - Rude-ee. Must be nice to know everything.