Skip to main content
Corrected answer in light of new information.
Source Link
Mark Smith
  • 2.2k
  • 1
  • 11
  • 14

AssumingYou say your switch is on the taped cable at the bottom of the screen, it seems to be connected between pin 7 and ground. This being the case, it's going to pull the signal low when pressed+5v. I think Therefore you want to enable the internal pullupwill need a pull-down resistor on that pin, and change the sense of the pin 7.

const int led1 = 6;
const int enable1 = 7;
boolean led1_OnOff;
long now;
int minSecsBetweenUpdates = 1; // 1 seconds
long lastSend = -minSecsBetweenUpdates * 1000l;

void setup() {
  led1_OnOff = false;
  pinMode(enable1, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
}

void loop() {
  now = millis();
  if (now > (lastSend + minSecsBetweenUpdates * 1000l))
  {
    if(digitalRead(enable1) == LOW)
    {
      lastSend = now;
      if(led1_OnOff == true)
      {
        led1_OnOff = false;
      }
      else
      {
        led1_OnOff = true;
      }
    }
    // Wait for button to be released
    while (digitalRead(enable1) == LOW) {}
  }

  if(led1_OnOff == true)
    digitalWrite(led1, HIGH);
  else
    digitalWrite(led1, LOW);
}

NoteYou say that Majenko's method of waiting for the buttonsetting pin 7 to be released is non-blocking and arguably therefore more elegant, butan output works. However you are very likely to burn out the issue hereoutput like this. A pull-down resistor is identifying the problem rather than code review, hence I've made minimal changesway to your code. I'd recommend you clean it up as per Majenko's suggestiongo.

Assuming your switch is on the taped cable at the bottom of the screen, it seems to be connected between pin 7 and ground. This being the case, it's going to pull the signal low when pressed. I think you want to enable the internal pullup on that pin, and change the sense of the pin.

const int led1 = 6;
const int enable1 = 7;
boolean led1_OnOff;
long now;
int minSecsBetweenUpdates = 1; // 1 seconds
long lastSend = -minSecsBetweenUpdates * 1000l;

void setup() {
  led1_OnOff = false;
  pinMode(enable1, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
}

void loop() {
  now = millis();
  if (now > (lastSend + minSecsBetweenUpdates * 1000l))
  {
    if(digitalRead(enable1) == LOW)
    {
      lastSend = now;
      if(led1_OnOff == true)
      {
        led1_OnOff = false;
      }
      else
      {
        led1_OnOff = true;
      }
    }
    // Wait for button to be released
    while (digitalRead(enable1) == LOW) {}
  }

  if(led1_OnOff == true)
    digitalWrite(led1, HIGH);
  else
    digitalWrite(led1, LOW);
}

Note that Majenko's method of waiting for the button to be released is non-blocking and arguably therefore more elegant, but the issue here is identifying the problem rather than code review, hence I've made minimal changes to your code. I'd recommend you clean it up as per Majenko's suggestion.

You say your switch is between pin 7 and +5v. Therefore you will need a pull-down resistor on pin 7.

You say that setting pin 7 to an output works. However you are very likely to burn out the output like this. A pull-down resistor is the way to go.

Source Link
Mark Smith
  • 2.2k
  • 1
  • 11
  • 14

Assuming your switch is on the taped cable at the bottom of the screen, it seems to be connected between pin 7 and ground. This being the case, it's going to pull the signal low when pressed. I think you want to enable the internal pullup on that pin, and change the sense of the pin.

const int led1 = 6;
const int enable1 = 7;
boolean led1_OnOff;
long now;
int minSecsBetweenUpdates = 1; // 1 seconds
long lastSend = -minSecsBetweenUpdates * 1000l;

void setup() {
  led1_OnOff = false;
  pinMode(enable1, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
}

void loop() {
  now = millis();
  if (now > (lastSend + minSecsBetweenUpdates * 1000l))
  {
    if(digitalRead(enable1) == LOW)
    {
      lastSend = now;
      if(led1_OnOff == true)
      {
        led1_OnOff = false;
      }
      else
      {
        led1_OnOff = true;
      }
    }
    // Wait for button to be released
    while (digitalRead(enable1) == LOW) {}
  }

  if(led1_OnOff == true)
    digitalWrite(led1, HIGH);
  else
    digitalWrite(led1, LOW);
}

Note that Majenko's method of waiting for the button to be released is non-blocking and arguably therefore more elegant, but the issue here is identifying the problem rather than code review, hence I've made minimal changes to your code. I'd recommend you clean it up as per Majenko's suggestion.