Hi,
I need to alternately blink two leds with the blink rate determined by a faster button and a slower button in the range of 0 to 9.
I am counting these button presses to a variable btnCount, incrementing / decrementing depending on which button is pressed. btnCount is then multiplied by 100 to give me an unsigned long blinkRate in a usable millisecond range.
I can use this as ‘delay (blinkRate);’ but it’s like glue and unusable. The blinking leds are so far behind the button presses.
I have failed comparing blinkRate to millis() to time my HIGH / LOW, and I have failed using a for loop with ‘counter < blinkRate’, before going HIGH when the condition goes false.
Any ideas?
Regards, Al.
The problem is in the code you didn't post.
Go back to the BlinkWithoutDelay example and start again.
Would it be easier to read a potentiometer where the resistance equates to blink rate?
.
You may try my code below.
const int blingFasterBottonPin = 7; // the pin number of fast button
const int blingSlowerBottonPin = 8; // the pin number of slow button
const int ledPin = 13; // the number of LED pin
boolean ledState = LOW;
bool blingFasterBotton = LOW;
bool blingSlowerBotton = LOW;
unsigned int blinkingDuration = 3000;
void setup() {
pinMode(blingFasterBottonPin, INPUT);
pinMode(blingSlowerBottonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin (9600);
}
void loop() {
unsigned long currentTime = millis();
blingFasterBotton = digitalRead(blingFasterBottonPin);
blingSlowerBotton = digitalRead(blingSlowerBottonPin);
//----------increase the blinking duration------------
const byte minimumBlinkingDuration = 100;
if (blingFasterBotton == HIGH && blingSlowerBotton == LOW && blinkingDuration > minimumBlinkingDuration) {
static unsigned long trackTime_B = 0;
if (currentTime - trackTime_B >= 5) {
blinkingDuration = blinkingDuration - 1;
trackTime_B = currentTime;
}
}
//----------------------------------------------------
//----------decrease the blinking duration------------
if (blingFasterBotton == LOW && blingSlowerBotton == HIGH) {
static unsigned long trackTime_C = 0;
if (currentTime - trackTime_C >= 5) {
blinkingDuration = blinkingDuration + 1;
trackTime_C = currentTime;
}
}
//--------------------------------------------------
//---------blink the led-----------------------
static unsigned long trackTime = 0;
if (currentTime - trackTime >= blinkingDuration) {
ledState = ! ledState;
// save the last time you blinked the LED
trackTime = currentTime;
Serial.println (blinkingDuration);
digitalWrite(ledPin, ledState);
}
//--------------------------------------------
}
Counting up / down buttons to control the blink rate of two less
Two less than what?
GrOnThOs:
You may try my code below.
It would be better to detect when the inputs become HIGH or LOW rather than when they are HIGH or LOW.
Hi,
Briefly: MorganS - yes, I'll post the code tonight. Apologies for being dim. Iee488 - the potentiometer works fine, but I need a definite blink rate. When I get this fixed I intend write the rate as a 'speed' to a seven segment display. Gr0nth0s - thank you. I will have a look at this tonight. PaulS - 'two leds'. I have sausages for fingers.
Kind regards,
Al.
UKHeliBob - thank you. A 'state change' rather than a 'state is'? I'll work on this. Kind regards, Al.
Physwiz:
UKHeliBob - thank you. A 'state change' rather than a 'state is'? I'll work on this. Kind regards, Al.
Look at the StateChangeDetection example in the IDE to see how it can be done.
PaulS - 'two leds'. I have sausages for fingers.
You CAN edit your post title.
It would be better to detect when the inputs become HIGH or LOW rather than when they are HIGH or LOW.
I think it's depend of what you want to do.
If you want increase or decrease the blinking duration each time you press or release the button, then it's important to detect the exact moment the input change value, but if you want the blinking duration continuously increase or decrease when you press and hold down the button, i think it's not so important to know the exact moment the input becomes HIGH or LOW.
but if you want the blinking duration continuously increase or decrease when you press and hold down the button, i think it's not so important to know the exact moment the input becomes HIGH or LOW.
Agreed, but it is important to control the rate of change of the duration.
Hi,
The counting in twos seems to be a hardware problem. Am awaiting new bits to see what's going on.
In the code below, int btnCount will in time be written to a seven segment display to give me a visual 0 to 9 'speed'. The button counting becomes stepSpeed by multiplying by 100 to give me something I can use in milliseconds, i.e. the display may read 4, equating to a 400ms blink rate. at present it writes to the serial monitor OK if I'm careful with the dodgy button. This all works fine if the for loop is commented out.
I'm trying to use the for loop, which goes false depending on the value of stepSpeed, to blink my LEDS. When I use this code and press the up button, it increments to 200ms in the serial monitor, both LEDs come on and that's it. Just sits there.
- thanks for the code, Gr0nth0s, I'm only doing it if I know it, I hope you understand!!
//declarations
const int leftBuz = 3;
const int rightBuz = 5;
const int upBtn = 7;
const int downBtn = 8;
int btnCount = 0;
int stepSpeed = 0;
int upBtnState = 0;
int downBtnState = 0;
int lastBtnState = 0;
void setup()
{
// put your setup code here, to run once:
pinMode (leftBuz, OUTPUT);
pinMode (rightBuz, OUTPUT);
pinMode (upBtn, INPUT);
pinMode (downBtn, INPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
upBtnState = digitalRead(upBtn);
downBtnState = digitalRead (downBtn);
unsigned long stepSpeed = btnCount * 100;
//first do the upButtonState checking
upBtnState = digitalRead(upBtn);
if (upBtnState != lastBtnState)
{
if (upBtnState == HIGH)
{
btnCount ++;
if (btnCount > 9)
{
btnCount = 9;
}
Serial.print ("the step speed is " );
Serial.println (stepSpeed);
}
delay (50);
}
lastBtnState = upBtnState;
//now do the downButtonState checking
downBtnState = digitalRead(downBtn);
if (downBtnState != lastBtnState)
// if they are different, now need to decrement the buttonCount
{
if (downBtnState == HIGH)
{
btnCount --;
if (btnCount < 0)
{
btnCount = 0;
}
Serial.print ("the step speed is " );
Serial.println (stepSpeed);
}
delay (50);
}
lastBtnState = downBtnState;
}
// analogWrite (leftBuz, 0);
// analogWrite (rightBuz, 0);
// for (int counter = 0; counter < stepSpeed; counter ++)
// {
// analogWrite (leftBuz, 0);
// analogWrite (rightBuz, 153);
// for (int counter = 0; counter < stepSpeed; counter++)
// {
// analogWrite (leftBuz, 153);
// analogWrite (rightBuz, 0);
// }
// }
//}
[/code]
Shouldn't there be a separate variable for the last button state for each button ?
Hi, I didn’t think so. My thinking is if you and a mate are throwing screwed up paper balls in a bin, or removing them, you’re only interested in how many paper balls are in the bin in total (btnCount)
Walk through the code on paper and write down the value of lastBtnState. After you check whether the up button has changed state you save the value then test whether the current value of the down button pin is different to the current value of the up button pin. The two are unrelated.
Surely you want to do
has the state of the up button changed and is it currently pressed ?
if so then increment the count and save the up button state
then
has the state of the down button changed and is it currently pressed ?
if so then decrement the count and save the down button state
The states of the two inputs are independant
Thanks. I’ll get on it when home