Hi there.
I need some help. I have completed the first two parts of my code blocks, tested it and all works fine, it is the last block of code I just cannot figure out where I am doing it wrong.
First the scope and some details:
I use internal Pull-up resistors with all switches.
ON-OFF-ON toggle switch, connected to Pins 3 and 4 (togglePin1 and togglePin2 respectively).
A momentary push button connected to Pin 2.
I have three conditions which are selected via an ON-OFF-ON toggle switch. The "first" on position is my "automatic" position where I cycle a 220VAC light bulb ON and OFF at 30 second intervals. (BTW, this was the initial scope, I wanted to add the three conditions). This condition, the "automatic" condition works a charm. For this condition I check via an IF statement whether togglePin1 is LOW, and togglePin2 is HIGH. I used a modified version of BlinkWithoutDelay since I want to check often if the toggle switch has been switched to OFF or the other ON.
Next, I have a "Nothing" condition. In this condition, nothing happens except the statusLed (pin 13) flashing. Here I check via an IF statement whether togglePin1 is HIGH and togglePin2 is HIGH. Again, works a charm. Again I used the BlinkWithoutDelay code for same reason as above.
The third mode I call "Trigger" mode. This condition leaves the light bulb off (led pin 12 at LOW) and waits for the push button to be pressed. I used the Debounce code to ensure that noise doesn't untrigger the cycling of the ON-OFF of the light bulb. This is my trouble code block that I cannot figure out.
I started this entire code thinking of using WHILE statements but couldn't get the IF statement to work with the pushbutton. Then I moved on to the multiple condition IF statements that include the timing for the BlinkWithoutDelay code. But I just cannot get the last bit to work. I have now split the last code block in two bits - first read the push button. Then I check via IF statement if togglePin1 is HIGH, and togglePin2 is LOW and if the debounce condition is met - meaning the pushbutton was pressed and it was not noise. Then I reset the pushbutton condition, change a the state of lightCycle from false to true. Then I proceed to the next if statement that, if the light cycle is true start with the bit of code that switches the light bulb ON-OFF. After that bit, there is another bit of code that checks again about the toggleswitch, and the puch button, and whether the lightCycle is true in order to change the lightCycle to false and switch off outputPin 12 - stopping the ON-OFF cycle.
Hope that makes sense, I've been sitting with it for the last 4 hours rewriting from my first code concept until this bit.
How do I do this last code block? How do I check the condition, plus check for a push button press without noise (debounce), start the lightCycle, and stop the lightCycle with another push button press all within the same toggleswitch condition. Currently, nothing happens. I've had it that the LED on pin 13 just flashes to just staying on, to I cannot remember all the other things that happened. I really want this last bit finished before this weekend Friday when I need to hand this project to the person I'm building it for. The basis blink sketch will do fine, but I really want this extra features for coolness and since I challenged myself with it. And I figured out a lot up until now that I am amazed with.
I do realise I can really stop and start the process with the toggle switch, but I like the idea of a push button trigger cause it is cooler and more ergonomic.
First, below my completed and tested bit of code with the "nothing" and "automatic" conditions.
Then the code block I just cannot figure out.
Completed code:
/*_________________Description & Scope______________________________________
This little program controls a incandescent light bulb for an exhibition
at a local church one of our friends go to. The initial design was merely
switching the light bulb on and off at 30 second intervals.
However, in conversation with my mate, we thought it will be nice to have
the option of triggering the cycle by means of a button. I decided to take
it one step further and provide three "modes" which are:
- Nothing: The light bulb is off with only a status light
- Automatic: The light bulb cycles indefinitly with no control except by
using the mode change switch
- Trigger: A push button triggers the start/stop of the cycle.
Although the "mode" switch can do the triggering of the cycle, which is a
ON-OFF-ON toggle switch, I think it is much more ergonomic to have a nice
red momentary push button to be the trigger switch. Plus it looks cooler.
The installation was temporary and as such I used my Seeeduino board v2.21
I used internal pull-up resistors cause its convenient and from past
experience, although I could be wrong, I have found that reading a HIGH
can cause false triggers since even static from my finger can cause noise
on the input pin, hence I now always pull-up to HIGH and read a LOW signal.
Critical parts used:
- BT139 600C TRIAC to switch the 220VAC and small heatsink for "coolness"
- MOS3022 Optocoupler to isolate the digital signals from the AC circuit
- ON-OFF-ON Toggle Switch
- Momentary Push Button switch
- Terminal Blocks
- ACDC Adapter set output of 9V DC as power source, wired into
input AC circuit
===========================================
= IMPORTANT! =
= Do NOT touch the heat sink or back =
= of the TRIAC when connected to AC =
= because the AC source lead is =
= connected to it. In other words, you =
= WILL GET ELECTROCUTED! =
===========================================
*/
/*___________________________Global Declarations________________________*/
const int togglePin1 = 3;
const int togglePin2 = 4;
const int buttonPin = 2;
const int statusPin = 13;
const int gatePin = 12;
int ledState = LOW;
int gateState = LOW;
long gateInterval = 3000;
long statusInterval = 500;
long previousMillis = 0;
long preGateMillis = 0;
/*_______________________________void setup_____________________________*/
void setup() {
pinMode(togglePin1, INPUT);
pinMode(togglePin2, INPUT);
pinMode(buttonPin, INPUT);
pinMode(statusPin, OUTPUT);
pinMode(gatePin,OUTPUT);
digitalWrite(togglePin1, HIGH);
digitalWrite(togglePin2, HIGH);
digitalWrite(buttonPin, HIGH);
digitalWrite(statusPin, LOW);
digitalWrite(gatePin, LOW);
}
void loop() {
/*__________________________IF nothing mode___________________________
When the toggle switch is in the OFF position, Nothing mode is engaged.
Nothing mode has a status LED that blinks.
*/
unsigned long statusMillis = millis();
if(statusMillis - previousMillis > statusInterval)
if (digitalRead(togglePin1) == HIGH
&& digitalRead(togglePin2) == HIGH)
{
digitalWrite(gatePin, LOW);
previousMillis = statusMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(statusPin, ledState);
}
/*__________________________IF automatic mode_______________________
When the toggle switch points downwards, Automatic mode is engaged. I check
for a OFF and ON condition of the toggle switch.
*/
unsigned long gateMillis = millis();
if(gateMillis - preGateMillis > gateInterval
&& digitalRead(togglePin1) == LOW
&& digitalRead(togglePin2) == HIGH)
{
preGateMillis = gateMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(statusPin, ledState);
if (gateState == LOW)
gateState = HIGH;
else
gateState = LOW;
digitalWrite(gatePin, gateState);
}
/*__________________________IF trigger mode_________________________
When the toggle switch points upwards, Automatic mode is engaged. I check
for a ON and OFF condition of the toggle switch.
*/
}