3

I try my code to change the state of a relay on pushing a push button buy checking the state of a digital pin.

My code :

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);
  pinMode(led1, OUTPUT);
}

void loop() {
  now = millis();
  if (now > (lastSend + minSecsBetweenUpdates * 1000l))
  {
    if(digitalRead(enable1) == HIGH)
    {
      lastSend = now;
      if(led1_OnOff == true)
      {
        led1_OnOff = false;
        //+digitalWrite(led1, LOW);
      }
      else
      {
        led1_OnOff = true;
        //digitalWrite(led1, HIGH);
      }
    }
  }

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

But the code keeps on looping and the relay keep changing it's state as if pin 7 is always high, I'm sure of my schematic but I don't know what's wrong with my code.

for those who want my schematic:

enter image description here

  • 1
    Did you wired pin 7? If it was floating, it will read as HIGH (mostly) and LOW sometimes. BTW your interval checking is incorrect, it doesn't work when value is expected to overflow (somewhere at 49th day). Checking the elapsed time (subtracted difference) with period value will work OK. – Flanker Jan 31 '17 at 3:59
  • Perhaps you should post your wiring diagram for bigger chance to get a proper answer – dhimaspw Jan 31 '17 at 6:29
  • 1
  • Did the code I gave you for your practically identical question yesterday help? – Mark Smith Jan 31 '17 at 8:19
  • no it didn't, my code was working but suddenly it has gone crazy, I even tried your code but it keeps on looping like enable1 is always high – Kareem Essam Gaber Jan 31 '17 at 12:02
2

Your problem is that you are basically saying "while the pin is HIGH toggle the relay" rather than "When, and only when, the pin transitions from LOW to HIGH do I toggle the relay".

To do that you must remember what state the pin was in during the last iteration:

const uint8_t led1 = 6;
const uint8_t enable1 = 7;

void setup() {
    pinMode(enable1, INPUT);
    pinMode(led1, OUTPUT);
}

void loop() {
    static bool led1_OnOff = false;
    static bool lastValue = digitalRead(enable1);

    bool currentValue = digitalRead(enable1);

    if (currentValue != lastValue) {
        lastValue = currentValue;
        if (currentValue == HIGH) {
            led1_OnOff = !led1_OnOff;
            digitalWrite(led1, led1_OnOff);
        }
    }
}
  • I tried that but I have the same problem, always keeps looping like crazy and only stops looping when I hit the push button – Kareem Essam Gaber Jan 31 '17 at 12:36
  • How have you wired up the pushbutton? – Majenko Jan 31 '17 at 13:41
  • It's not a small push button for arduino, it's a normal lighting AC push button, I user one side for pin 7 and the other for 5V – Kareem Essam Gaber Jan 31 '17 at 13:43
  • So no pulldown resistor? No wonder it's spazzing out - the pin is floating until you press the button and connect it to a signal of some form. Read the basic Arduino button tutorial: arduino.cc/en/Tutorial/Button – Majenko Jan 31 '17 at 13:44
  • that was the problem, thanks for pointing it out...I didn't know I had to use a resistor for the AC standard push button like the arduino small push button. – Kareem Essam Gaber Jan 31 '17 at 14:06
1

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.

  • no it's connected to 5V and pin 7...I think I solved it by setting enable1 pin to OUTPUT in the setup() loop and forcing it to start at low state by calling digitalWrite(enable1, LOW); – Kareem Essam Gaber Jan 31 '17 at 13:11
  • In that case, you will need a pull DOWN resistor. – Mark Smith Jan 31 '17 at 13:31
  • no that's only what I did, when I set enable1 to output it goes crazy but when I set it to input it's fine. – Kareem Essam Gaber Jan 31 '17 at 13:33
  • void setup() { led1_OnOff = false; pinMode(enable1, OUTPUT); digitalWrite(enable1, LOW); pinMode(led1, OUTPUT); } void loop() { now = millis(); if (now > (lastSend + 1000)) { if(digitalRead(enable1) == HIGH) { lastSend = now; if(led1_OnOff == true) led1_OnOff = false; else led1_OnOff = true; } } if(led1_OnOff == true) digitalWrite(led1, HIGH); else digitalWrite(led1, LOW); } – Kareem Essam Gaber Jan 31 '17 at 13:37
  • OK, I understand what's happening now. Answer updated. You'll blow up your Arduino if you do it like that. – Mark Smith Jan 31 '17 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.