Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.
Does the instructor explain why you are to use PWM pins for lights that are only ON or OFF?
Hint:
Use an 'unsigned long int' variable to record the time in milliseconds (returned by the millis() function) when the button goes from LOW to HIGH. When the button goes from HIGH to LOW, subtract the recorded time from the value returned by millis() to get the duration that the button input was HIGH. If that value if >=500 and <=1000 you toggle the states of the two lights.
rlclement:
The switch would go zero to one to zero (ground to +5 volts) momentary with a duration of 500ms to 1 second.
Does that mean that if the switch is pressed for less than 500mS, or more than 1 second, that it should be ignored? It is fairly unusual to have a timing specification on a manual switch, unless you are intending to perform different actions based on the length of time the switch is pressed.
int Switch = 2; //tactile toggle switch
int ledg = 8; //green LED
int ledr = 9; //red LED
int state = 0, Loadstate = 0;
void setup()
{
pinMode(Switch, INPUT);
pinMode(ledg, OUTPUT);
pinMode(ledr, OUTPUT);
}
void loop()
{
if (state == 0 && digitalRead(Switch) == HIGH) {
state = 1;
Loadstate = !Loadstate;
}
if (state == 1 && digitalRead(Switch) == LOW) {
state = 0;
}
if (Loadstate == HIGH) {
digitalWrite (ledg, LOW);
digitalWrite (ledr, HIGH);
}
if (Loadstate == LOW) {
digitalWrite (ledr, LOW);
digitalWrite (ledg, HIGH);
}
}
the switch duration (500ms to 1s) is a moot point.
I used PWM to control LED current and brightness, the code I am using now is not using PWM.
The tactile switch operates as a toggle and uses a pull up resistor, switch output is normally = 1, when pressed = 0.
The code should start with green LED ON, push tactile switch, no change, release switch green LED turns OFF and red LED turns ON, actuate switch again reiterates start state.
The problem is inconsistent results, most of the time the LED's switch state, sometimes not,
const byte SwitchPin = 2; // tactile toggle switch
const byte GreenLEDPin = 8;
const byte RedLEDPin = 9;
boolean GreenOn = true;
void setup()
{
pinMode(SwitchPin, INPUT_PULLUP); // This eliminates the need for an external pull-up resistor
pinMode(GreenLEDPin, OUTPUT);
digitalWrite(GreenLEDPin, HIGH); // Start with Green on...
pinMode(RedLEDPin, OUTPUT);
digitalWrite(RedLEDPin, LOW); //... and Red off.
}
void loop()
{
boolean switchPressed = digitalRead(SwitchPin) == LOW; // Active Low
static boolean switchWasPressed = false;
static unsigned long debounceTimer = 0;
// Look for a state change
if (switchPressed != switchWasPressed && millis() - debounceTimer >= 10)
{
// The switch has changed state
switchWasPressed = switchPressed;
debounceTimer = millis();
if (!switchPressed)
{
// The switch was just released
// Toggle the state of the lights
GreenOn = !GreenOn;
// Display the new state
if (GreenOn)
{
digitalWrite(GreenLEDPin, HIGH); // Green on...
digitalWrite(RedLEDPin, LOW); //... and Red off.
}
else
{
digitalWrite(GreenLEDPin, LOW); // Green off...
digitalWrite(RedLEDPin, HIGH); //... and Red on.
}
}
}
}