millis() problem

Hi to all,

My goal is to interface a single digit 7 seg display with an I/O expander module and an Arduino pro mini.My code posted below is just some test code i have put together from other posts found here and it works as expected except i need the LED to turn off after a 1 minute interval has expired in CHOICE_1().This is where i am having problems the LED just stays on.I have found that if i change the line "Pressed = true;" in the else if part of my code to "Pressed == true;" the LED will go off as expected but then it will not come on when the function is called again.Ijust can't get my head around this and am hoping that one of you guys can point me in the right direction.Thank You.

#include "Wire.h"
 #define Expander 0x20
int Switchcounter = 0;
const int Switch = 5;
unsigned long previousMillis = 0;
unsigned long interval = 60000;

void setup() {

  Wire.begin();
  pinMode (Switch, INPUT);
}

void CHOICE() {
  //All off
  Wire.beginTransmission(0x20);
  Wire.write(B11111111);
  Wire.endTransmission();

}

void CHOICE_1() {

  unsigned long currentMillis = millis();
  static boolean Pressed = true;
  if (Pressed == true) {
    // Swicth the LED on
    Wire.beginTransmission(0x20);
    Wire.write(B11111110);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed = false;
  }

  else if ( currentMillis - previousMillis > interval) {
    // switch the LED off
    Wire.beginTransmission(0x20);
    Wire.write(B11111111);
    Wire.endTransmission();
    Pressed = true;
  }
}

void loop() {

  int SwitchState = digitalRead(Switch);
  if (SwitchState == LOW) {
    delay(200);
    Switchcounter ++;
  }
  if (Switchcounter == 2)
    Switchcounter = 0;
  if (Switchcounter == 0 ) 
    CHOICE();
  else if (Switchcounter == 1)
    CHOICE_1();

  }

What is the function supposed to do?

You need to start by looking at the state change detection example. You want to increment the counter only when the switch state changes to pressed.

You also need to explain HOW the switch is wired.

currentMillis is a lousy name. now would be better. previosMillis is a lousy name. valueSent would be better.

PaulS: valueSent would be better.

That makes it sound like its the value he sent. Should have "time" in there somehow to denote it as representing a time. Maybe lastMessageTime or lastSendTime.

currentMillis I'm cool with. It says what it is, the "current" time from "millis". Not to be confused with any actual time from something like the Time library or an RTC. currentTimeInMillis might be more descriptive, but hell you could go all out and name it theNumberWhatWasReturnedFromThatThereFunctionMillisTheLastTimeICalledIt.

Also the switch is wired through a PULLUP resistor.

Vague handwaving doesn't cut it here. A schematic does.

I have tried to incorporate the state change detection code but i just cannot get it to work.

Vague handwaving doesn't cut it here. The code that doesn't work does.

Paul thanks for your reply and time i appreciate it but my switch is wired correctly the hardware side of things is not a problem to me.I didn’t save my code for the state change example as it did not work at all and the code i have posted gets me nearer to where i want to be that’s why i posted it.I am not wanting people to write the code for me just some guidance on how i would modify my posted code to do the job.

if i change the line "Pressed = true;" in the else if part of my code to "Pressed == true;"

Do you understand the difference between = and == ?

Yes Bob i understand the difference that was just an observation i made while trying to correct my problem

You do turn the LED off, but you immediately turn it back on again by setting Pressed = true;. Reply #2 has the solution, you don't implement any state change detection.

aarg thank you for you reply can you please help me with modifying my posted code as i have been trying to implement the changes required as Paul’s post suggested but i just cant get it to work.I am trying but coding is all new to me.I just can get where or how to implement the state change code.

I just can get where or how to implement the state change code.

Have you LOOKED at the state change detection example? Have YOU tried anything in terms of implementing it in YOUR code? This is NOT the homework hotline. We will discuss what is wrong with your code; we will not write the code for you.

Paul i have looked and tried believe me and as i don't really understand the code i'm not sure what parts of that code i need or where to insert them in my code.If you could just direct me a little i'm very willing and keen to learn.

You have code that reads the state of a switch. Delete it. Replace it with the code from the state change detection example.

int prevState;

void loop()
{
   int currState = digitalRead(somePin);
   if(currState != prevState)
   {
      // The state changed. We might want to increment the counter, or we might not
      if(currState == LOW) // Or HIGH, whichever means pressed
      {
         // We DO want to increment the counter
      }
   }

   // Do whatever else needs doing, based on the value of the counter
}

Thank you Paul i will work with that and will let you know how i get on.

Hi again Paul i have finally got my code to behave as expected and Thank You for your help,another bit learned (my first bit of code without a delay in sight :slight_smile: ).I am sure that it could be written much more efficiently but it works and it’s a start.For anyone that may find it useful i am using a PCF8574 module to drive a single digit 7 seg display.The code will display the value of a switch input from 0-9 and save it to EEPROM as i need it to restart at same value as was on power off ,however each digit is only displayed for 20 seconds and it will turn off.The switch is also debounced with software.

#include <Wire.h>
#include <EEPROM.h>
#define PCF 0x20
byte Switchcounter = 0;
byte oldSwitchState = HIGH;
int address = 0;
int State;
const int SwitchPin = 4;
unsigned long previousMillis = 0;
unsigned long interval = 20000;
unsigned long switchPressTime;
const unsigned long debounceTime = 100;

void setup() {

  Wire.begin();
  Switchcounter = EEPROM.read(address);
  Wire.beginTransmission(PCF);
  Wire.write(Switchcounter);
  Wire.endTransmission();
  pinMode(SwitchPin, INPUT);
}

void OPTION() {

  unsigned long currentMillis = millis();
  static boolean Pressed = true;
  if (Pressed == true) {
    // Swicth Digit 0 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x81);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed = true;
}

void OPTION_1() {

  unsigned long currentMillis = millis();
  static boolean Pressed1 = true;
  if (Pressed1 == true) {
    // Swicth Digit 1 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0xED);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed1 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed1 = true;
}

void OPTION_2() {

  unsigned long currentMillis = millis();
  static boolean Pressed2 = true;
  if (Pressed2 == true) {
    // Swicth Digit 2 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0xC2);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed2 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed2 = true;
}

void OPTION_3() {

  unsigned long currentMillis = millis();
  static boolean Pressed3 = true;
  if (Pressed3 == true) {
    // Swicth Digit 3 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x92);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed3 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed3 = true;
}

void OPTION_4() {

  unsigned long currentMillis = millis();
  static boolean Pressed4 = true;
  if (Pressed4 == true) {
    // Swicth Digit 4 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0xB4);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed4 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed4 = true;
}

void OPTION_5() {

  unsigned long currentMillis = millis();
  static boolean Pressed5 = true;
  if (Pressed5 == true) {
    // Swicth Digit 5 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x98);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed5 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed5 = true;
}

void OPTION_6() {

  unsigned long currentMillis = millis();
  static boolean Pressed6 = true;
  if (Pressed6 == true) {
    // Swicth Digit 6 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x88);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed6 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed6 = true;
}

void OPTION_7() {

  unsigned long currentMillis = millis();
  static boolean Pressed7 = true;
  if (Pressed7 == true) {
    // Swicth Digit 7 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0xB3);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed7 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed7 = true;
}

void OPTION_8() {

  unsigned long currentMillis = millis();
  static boolean Pressed8 = true;
  if (Pressed8 == true) {
    // Swicth Digit 8 on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x80);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed8 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed8 = true;
}

void OPTION_9() {

  unsigned long currentMillis = millis();
  static boolean Pressed9 = true;
  if (Pressed9 == true) {
    // Swicth Digit 9+fullstop on 7seg display on
    Wire.beginTransmission(PCF);
    Wire.write(0x10);
    Wire.endTransmission();
    previousMillis = currentMillis;
    Pressed9 = false;
  }
  else if ( currentMillis - previousMillis > interval) {
    // Swicth 7seg display off
    Wire.beginTransmission(PCF);
    Wire.write(0xFF);
    Wire.endTransmission();
  }
  State = digitalRead(SwitchPin);
  if (State == LOW)
    Pressed9 = true;
}

void loop() {

  byte switchState = digitalRead (SwitchPin);
  if (switchState != oldSwitchState) {

    if (millis () - switchPressTime >= debounceTime)
    {
      switchPressTime = millis ();
      oldSwitchState =  switchState;
      if (switchState == LOW) {
        Switchcounter++;
        EEPROM.write(address, Switchcounter);
      }
    }
  }

  if (Switchcounter == 10)
    Switchcounter = 0;
  if (Switchcounter == 0 )
    OPTION();
  else if (Switchcounter == 1)
    OPTION_1();
  else if (Switchcounter == 2 )
    OPTION_2();
  else if (Switchcounter == 3 )
    OPTION_3();
  else if (Switchcounter == 4 )
    OPTION_4();
  else if (Switchcounter == 5 )
    OPTION_5();
  else if (Switchcounter == 6 )
    OPTION_6();
  else if (Switchcounter == 7 )
    OPTION_7();
  else if (Switchcounter == 8 )
    OPTION_8();
  else if (Switchcounter == 9 )
    OPTION_9();
}