I am working with this code on UNO for a one shot or functions like a mono stable works with a very short duration of time
(Just A Tick) . .
I want to design the system without delay so going with the code below "
const int SWT1 = 3; // the number of the pushbutton pin
const int LED1 = 2;
const int DEBOUNCE_DELAY = 10; // the debounce time; increase if the output flickers
// Variables will change:
int lastSteadyStateA = LOW; // the previous steady state from the input pin
int lastFlickerableStateA = LOW; // the previous flickerable state from the input pin
int currentStateA; // the current reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(SWT1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
}
void loop() {
// read the state of the switch/button:
currentStateA = digitalRead(SWT1);
if (currentStateA != lastFlickerableStateA) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableStateA = currentStateA;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
if (lastSteadyStateA == HIGH && currentStateA == LOW)
Serial.println("The button is pressed");
if(!digitalRead(SWT1))
{
digitalWrite(LED1, lastSteadyStateA);
}
lastSteadyStateA = currentStateA;
}
}
Problem is :-
Its not always working
OR
I have to wait for 4 to 5 seconds to make action again
Too with fast switching of micro switch button
for 5 clicks it works for only 2 clicks hardly
Ya everything is fine here and with a delay it works very well
I am on it right now
just trying to modify a code obtained from one of this forum post only
Here it is . . .
const int SWT1 = 3; // the number of the pushbutton pin
const int LED1 = 2;
int start;
int flag;
int lastMillis;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(SWT1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
// digitalWrite(LED1, HIGH);
}
void loop() {
if(!digitalRead(SWT1))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED1, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
flag = true; // this enables the timing checking
}
if (flag == true && millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED1, LOW); // turn led off
flag = false; // this disables timing checking
}
}
}
LED is not always responding to the fullest thus not able to trigger something connected as extension there , say a 100 watt bulb with a triac or a motor with a driver
Just examples of extension I am telling . . . . !!!!
I just checked it on Serial Monitor
It shows result but the LED is not always glowing and at the same moment any other load connected is also not working
Don't know whats going on actually !!!
So far I understand:
You want your project to be a single shot at the push of a button.
From here the questions remain:
When you fire the shot, you want it to be ready again for another shot, or you want it to prevent another shot.
If you want a new shot,
want it to be immediately ready for another shot,
or do you want there to be time between the end of a shot and the possibility of the next shot?
Shot is going well on board and serial monitors
But all the time LED connected to PIN number 2 is not glowing
But on serial monitor its working very well
and just now tested with MEGA
There also a Red SMD LED is responding means the picture is clear with code very well . . . .
Don't why output is having this kind of abnormality !!
I just want to have a one shot LED glow and OFF
Like a blink of eye
And ya ready for another shot
No matter how many times or how fast I use to trigger with micro switch, that output should respond all the time !!!
Ya Sir with this code below I started one by one for 16 entries
have a look :
const int SWT1 = 22; // the number of the pushbutton pin
const int SWT2 = 24;
const int SWT3 = 26;
const int SWT4 = 28;
const int SWT5 = 30;
const int SWT6 = 32;
const int SWT7 = 34;
const int SWT8 = 36;
const int SWT9 = 38;
const int SWT10 = 40;
const int SWT11 = 42;
const int SWT12 = 44;
const int SWT13 = 46;
const int SWT14 = 48;
const int SWT15 = 50;
const int SWT16 = 52;
const int LED1 = 23;
const int LED2 = 25;
const int LED3 = 27;
const int LED4 = 29;
const int LED5 = 31;
const int LED6 = 33;
const int LED7 = 35;
const int LED8 = 37;
const int LED9 = 39;
const int LED10 = 41;
const int LED11 = 43;
const int LED12 = 45;
const int LED13 = 47;
const int LED14 = 49;
const int LED15 = 51;
const int LED16 = 53;
int start;
int lastMillis;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(SWT1, INPUT_PULLUP);
pinMode(SWT2, INPUT_PULLUP);
pinMode(SWT3, INPUT_PULLUP);
pinMode(SWT4, INPUT_PULLUP);
pinMode(SWT5, INPUT_PULLUP);
pinMode(SWT6, INPUT_PULLUP);
pinMode(SWT7, INPUT_PULLUP);
pinMode(SWT8, INPUT_PULLUP);
pinMode(SWT9, INPUT_PULLUP);
pinMode(SWT10, INPUT_PULLUP);
pinMode(SWT11, INPUT_PULLUP);
pinMode(SWT12, INPUT_PULLUP);
pinMode(SWT13, INPUT_PULLUP);
pinMode(SWT14, INPUT_PULLUP);
pinMode(SWT15, INPUT_PULLUP);
pinMode(SWT16, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
pinMode(LED14, OUTPUT);
pinMode(LED15, OUTPUT);
pinMode(LED16, OUTPUT);
}
void loop() {
if (!digitalRead(SWT1))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED1, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED1, LOW); // turn led off
start = false;
while(!digitalRead(SWT1)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT2))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED2, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED2, LOW); // turn led off
start = false;
while(!digitalRead(SWT2)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT3))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED3, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED3, LOW); // turn led off
start = false;
while(!digitalRead(SWT3)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT4))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED4, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED4, LOW); // turn led off
start = false;
while(!digitalRead(SWT4)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT5))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED5, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED5, LOW); // turn led off
start = false;
while(!digitalRead(SWT5)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT6))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED6, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED6, LOW); // turn led off
start = false;
while(!digitalRead(SWT6)){}
delay(1); // For debouncing
}
if (!digitalRead(SWT7))
{
if (start == false) // do we want to start the LED on/off cycle?
{
lastMillis = millis(); // initialize the timing
digitalWrite(LED7, HIGH); // turn LED on
start = true; // this prevents reinitializing lastMillis
}
}
if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
{
digitalWrite(LED7, LOW); // turn led off
start = false;
while(!digitalRead(SWT7)){}
delay(1); // For debouncing
}
// if (!digitalRead(SWT8))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED8, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED8, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT8)){}
// delay(1); // For debouncing
// }
}
Now up to 7th pair its working fine
But introducing the 8th one damage everything . . .
Here I mean, all the outputs are simply getting HIGH on pressing & holding the switch and remains in that HIGH state only
Now, when the switch gets released, it goes OFF
For all the 8 Inputs this type of function is going on
After blocking the code for 8th pair all the 7 outputs are working very well
In the code above I blocked the 8th on as you can check there . . .
Well I worked on WOKWI only
No practical implementation till now due to lack of few components presently