im trying to build a simple code but it become too complicated for me..
i need :
when i close a switch to power on a led with delay 5 sec and when i close the switch to power off the led with delay of 500 millisecond
so up to here ok i put currentMillis = millis(); in the code all just perfect
if i close the switch more than 5 times in 10 minutes i need to keep the led off
or power on another led ...
and if 10 minutes pass the counter(how many times the switch become on ) to go to 0 and start 10 minutes timer from the beginning
here my code ... im using arduino uno
and here is the sequence
switch on --> 5sec later led on ....switch off --->500ms later led off...if switch more than 5 times in 10 minutes then led locked to off or another led on....if the 10 minutes pass and only 3 times the switch is on then the counter start over(reset to 0 times) ..
const int buttonPin = 2;// the button (switch)
const int ledPin = 12; ///led
const int ledPin1 = 11; ///only for my own tests
int buttonState = 0;
unsigned long interval = 5000; // how long will pass from the switch on to power on the led
unsigned long previousMillis = 0;
int currentState = 0;
int buttonPushCounter = 0; // counter for the number of button presses
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);//look at INPUT_PULLUP
//buttonState = digitalRead(2);
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();//millis is the internal timer
//Serial.println(currentMillis);
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin, HIGH);
}
}
else {
previousMillis = currentMillis; //if button not pressed keep updating time stamp
delay(500); //off time delay
digitalWrite(ledPin, LOW);
}
if (buttonState != lastButtonState) {// if the state has changed, increment the counter
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
Serial.println(currentMillis);
} else {
//Serial.println(currentMillis);
}
// Delay a little bit to avoid bouncing
//delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 5 == 0) {
digitalWrite(11, HIGH);
} else {
}
}
To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum
My text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.
Also please use the AutoFormat tool to indent your code for easier reading.
You need to tell us in detail what your program actually does and what you want it to do that is different.
OP, what you're asking for actually seems fairly complex. Here's something that compiled on a 2560 but probably has holes and bugs in it. See if it's any use to you:
//and here is the sequence
//switch on --> 5sec later led on ....
//switch off --->500ms later led off...
//if switch more than 5 times in 10 minutes then led locked to off ... (before another led on....)
//if the 10 minutes pass and only 3 times the switch is on then the counter start over(reset to 0 times) ..
const int buttonPin = 2;// the button (switch)
const int ledPin_Green = 12; ///led
const int ledPin_Red = 11; ///only for my own tests (co-opted for use as BLOCKED indicator...)
int buttonState = 0;
int buttonPushCounter = 0; // counter for the number of button presses
int lastButtonState = 0; // previous state of the button
unsigned long
timerLEDTransition,
timerButtonRead,
timerButtonSwLimit;
bool
bInitialBTNOff,
bfirstSwitchOn;
byte
stateLED;
#define LED_OFF HIGH
#define LED_ON LOW
#define OPEN LOW
#define CLOSED HIGH
#define INTERVAL_BTN_READ 20 //mS time between switch reads
#define LED_OFF_DELAY 500 //mS time from button 'off' to LED off
#define LED_ON_DELAY 5000L //mS time from button 'on' to LED on
#define WINDOW_BTN_PRESSES 5 //# numer of off to on button presses allowed in SW_LIMIT_WINDOW mS
#define SW_LIMIT_WINDOW 600000L //mS window of time for limiting # of presses from 1st press
#define STATE_LED_IDLE 0
#define STATE_LED_ON 1
#define STATE_LED_OFF 2
#define STATE_LED_BLOCK 3
void setup()
{
pinMode(ledPin_Green, OUTPUT);
digitalWrite( ledPin_Green, LED_OFF );
pinMode(ledPin_Red, OUTPUT);
digitalWrite( ledPin_Red, LED_OFF );
pinMode(buttonPin, INPUT_PULLUP);//look at INPUT_PULLUP
//initialize button logic
//want to wait until we see the button "off" to sync up
stateLED = STATE_LED_IDLE;
timerButtonRead = millis();
buttonState = digitalRead(buttonPin);
buttonPushCounter = 0;
bfirstSwitchOn = false;
bInitialBTNOff = false;
lastButtonState = buttonState;
if( buttonState == LOW )
bInitialBTNOff = true;
Serial.begin(9600);
while(!Serial);
}//setup
void loop()
{
ReadButton();
Check10minTimer();
LEDControl();
}//loop
/////////////////////////////////////////////////////////////////////////////////
//Check10minTimer
// Checks to see if the 10-minute window period has ended when at least
// one button press has been detected.
/////////////////////////////////////////////////////////////////////////////////
void Check10minTimer( void )
{
if( buttonPushCounter >= 1 )
{
if( millis() >= timerButtonSwLimit )
buttonPushCounter = 0;
}//if
}//Check10minTimer
/////////////////////////////////////////////////////////////////////////////////
// ReadButton
// Checks for changes in state of the button.
// Button is checked once every INTERVAL_BTN_READ mS
//
// Once a button change of state is detected it is acted upon and its
// condition is allowed to run to completion before the next switch state
// checked and acted on.
/////////////////////////////////////////////////////////////////////////////////
void ReadButton( void )
{
unsigned long
timeNow = millis();
if( timeNow < timerButtonRead )
return;
timerButtonRead = timeNow + INTERVAL_BTN_READ;
//only process buttons when no other action is in progress
//e.g. delay before turning LED on or off or if locked out
if( stateLED != STATE_LED_IDLE )
return;
//button must be known to be "off" initially before processing
if( !CheckFirstBTNLow() )
return;
buttonState = digitalRead(buttonPin);
if( buttonState != lastButtonState )
{
lastButtonState = buttonState;
switch( buttonState )
{
case CLOSED: //button 'on'
buttonPushCounter++;
//if we're still within 10-min window, check for excessive button presses
if( (millis() < timerButtonSwLimit) && (buttonPushCounter > WINDOW_BTN_PRESSES) )
{
//too many button presses - lock out
stateLED = STATE_LED_BLOCK;
return;
}//if
//move to state that turns on the LED
timerLEDTransition = timeNow + LED_ON_DELAY;
stateLED = STATE_LED_ON;
//if this is the first low-to-high transition, re-start the window timer
if( buttonPushCounter == 1 )
timerButtonSwLimit = millis() + SW_LIMIT_WINDOW;
break;
case OPEN: //button 'low'
//button has changed from high to low
//set the off-delay and schedule the off state
timerLEDTransition = timeNow + LED_OFF_DELAY;
stateLED = STATE_LED_OFF;
break;
}//switch
}//if
}//ReadButton
/////////////////////////////////////////////////////////////////////////////////
// CheckFirstBTNLow
// Checks for the button 'low' to start processing. If the program is started
// with the switch in the 'on' state this logic waits for it to be set 'off'
// before doing anything else.
/////////////////////////////////////////////////////////////////////////////////
bool CheckFirstBTNLow( void )
{
if( bInitialBTNOff == true )
return bInitialBTNOff;
buttonState = digitalRead(buttonPin);
if( buttonState == LOW )
{
bInitialBTNOff = true;
lastButtonState = buttonState;
return( true );
}//if
else
return( false );
}//CheckFirstBTNLow
/////////////////////////////////////////////////////////////////////////////////
// LEDControl
// Simple state machine to handle LED timing and control.
// A "lock-out" is provided that lights the red LED and prevents further
// green LED activity if more than WINDOW_BTN_PRESSES button presses are
// detected within SW_LIMIT_WINDOW mS.
/////////////////////////////////////////////////////////////////////////////////
void LEDControl( void )
{
unsigned long
timeNow = millis();
switch( stateLED )
{
case STATE_LED_IDLE:
//do nothing in idle state
//change of state happens in button detection
break;
case STATE_LED_ON:
if( timeNow < timerLEDTransition )
return;
digitalWrite(ledPin_Green, LED_ON);
stateLED = STATE_LED_IDLE;
break;
case STATE_LED_OFF:
if( timeNow < timerLEDTransition )
return;
digitalWrite(ledPin_Green, LED_OFF);
stateLED = STATE_LED_IDLE;
break;
case STATE_LED_BLOCK:
digitalWrite(ledPin_Red, LED_ON);
//if blocked, no exit from this state
break;
}//switch
}//LEDControl