programming UNO

Hello,

I need a little help programming Arduino UNO, it seems like it should be simple and probably is for anyone using C or C++.

The hardware would be 1 momentary switch, 1 red LED and 1 blue LED.

Both LED would be controlled by PWM’s

The switch would go zero to one to zero (ground to +5 volts) momentary with a duration of 500ms to 1 second.

  1. The sketch would begin, turning ON the blue LED,
  2. When the switch is actuated, the blue LED would turn OFF and the red LED would turn ON
  3. When the switch is actuated again, red LED OFF and blue LED ON.

I need help with the switch software, hints or anything else.

Thank You
Richard

Show us what code that you have tried. Describe what the code does and how that differs from what you want to do.

How to wire a switch.

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.

This might be useful:
PushButton-debounce.ino

Pieter

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,

I think I will try a switch debounce :slight_smile:

This adds more meaningful names and debounce:

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.
      }
    }
  }
}

thank you johnwasser, your help is very much appreciated.