Hi all,I am trying to control 2 LED'S via i2c with a PCF8574 module and UNO with a momentary switch connected to pin 2 via PULLUP resistor.The code below works fine except i would like the led in OPTION_1() to be turned off after seven seconds without using the delay function but i just cannot get this part of my code to work.Any advice on where i'm going wrong would be much appreciated.
I have added some code to your code.
Observe how many times OPTION_1 debugging prints get executed.
You may want to add some identifying prints too - Serial.print( " previousMillis = "); etc and also you may want to use debug print when "if" condition is false.
Especially pay attention to initial value of previousMillis and changes , if any.
Reply / let the group know about your progress.
Good luck
#include "Wire.h"
#include <EEPROM.h>
#define PCF 0x20
int Switchcounter = 0;
int address = 0;
const int SwitchPin = 2;
unsigned long previousMillis = 0;
unsigned long interval = 7000;
void setup()
{
Initialze Serial here
Serial.begin(115200);
Wire.begin();
pinMode (SwitchPin, INPUT);
}
void OPTION()
{
Wire.beginTransmission(PCF);
Wire.write(0xFF);
Wire.endTransmission();
}
void OPTION_1()
{
Add static counter here
static int TestCOunter;
Wire.beginTransmission(PCF);
Wire.write(0xFE);
Wire.endTransmission();
unsigned long currentMillis = millis();
Insert debugging code here
Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(interval);
Serial.println(TestCOunter++);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Wire.beginTransmission(PCF);
Wire.write(0xFF);
Wire.endTransmission();
and for test purpose here too
Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(interval);
}
}
void OPTION_2()
{
Wire.beginTransmission(PCF);
Wire.write(0xFC);
Wire.endTransmission();
}
void loop() {
int SwitchState = digitalRead(SwitchPin);
if (SwitchState == LOW) {
delay(200);
Switchcounter ++;
EEPROM.write(address, Switchcounter);
delay(10);
}
if (Switchcounter == 3) {
Switchcounter = 0;
EEPROM.write(address, Switchcounter);
delay(20);
}
if (Switchcounter == 0 ) {
OPTION();
}
else if (Switchcounter == 1 ) {
OPTION_1();
}
else if (Switchcounter == 2 ) {
OPTION_2();
}
}
My version Keep a static flag that you want to start (switch led on) or wait 7 seconds before switching the led off.
void OPTION_1()
{
static bool start = true;
unsigned long currentMillis = millis();
if (start == true)
{
// swicth LED on
Wire.beginTransmission(PCF);
Wire.write(0xFE);
Wire.endTransmission();
// set previous 'time' to current 'time'
previousMillis = currentMillis;
// indicate that we've switched the led on
start = false;
}
else
{
// compare current 'time' with previous 'time'
if (currentMillis - previousMillis >= interval) {
// switch LED off
Wire.beginTransmission(PCF);
Wire.write(0xFF);
Wire.endTransmission();
// for the next time we call this function, indicate that we must switch the led on
start = true;
}
}
}
long previousMillis = 0;
long interval = 7000;
unsigned long currentMillis;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
OPTION_1();
//for(;;);
}
void OPTION_1()
{
static bool start = true;
currentMillis = millis();
Serial.print (" currentMillis = ");
Serial.println (currentMillis);
Serial.print (" previousMillis = ");
Serial.println (previousMillis);
Serial.print (" static start = ");
Serial.println (start);
delay(1000);
if (start == true)
{
Serial.println("switch LED on");
// Wire.beginTransmission(PCF);
// Wire.write(0xFE);
// Wire.endTransmission();
// set previous 'time' to current 'time'
previousMillis = currentMillis;
// indicate that we've switched the led on
Serial.print (" new previousMillis = ");
Serial.println (previousMillis);
start = false;
}
else
{
// compare current 'time' with previous 'time'
if (currentMillis - previousMillis >= interval) {
Serial.println(" switch LED off");
// Wire.beginTransmission(PCF);
// Wire.write(0xFF);
// Wire.endTransmission();
// for the next time we call this function, indicate that we must switch the led on
start = true;
}
}
}
Why do you expect people to help you if you act like a jerk?
You have not been providing us with information that we've requested - when you're asking for help, and people trying to help you ask a question, they're doing that because they feel they need the answer to the question to help you. You still have not answered Rob's question.
You also need to learn how to post on forums in a manner that provides people the information they need to help you. This is a great thing to read, it will teach you how to ask questions on forums in a way that gets answers and doesn't piss people off - and in this day and age, that's very powerful: How To Ask Questions The Smart Way