2 LEDS ON/OFF and Push button to turn off one of them

image
Thats a green and a red led , they will light every high edge , I use delay function to do this wave for both of them
digitalWrite(4, LOW); // turn the red LED on
digitalWrite(5, HIGH); // turn the green LED on
delay(1000);
ect...

but I want as next step to install a button that will turn of green led when i press it without affecting the red light . I was thinking to code it with pulsln() but i dont have periodic signal (red led). (info to know : The green/red led will have only 5 steps as shown on picture.)
The problem is : They told me its easy to code it but my mind said "timers party"
Anyway...thanks for reading guys ! <3

1 Like

Close.
"millis() party".

1 Like

Hello andreas_hacked
Post your current sketch, well formated, with comments and in so called code tags "</>" and a schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.
Дайте миру шанс

1 Like

Hi,
I didn't quite understand this part:

they will light every high edge

digitalWrite(4, LOW); // turn the red LED on ...................... LOW led ON????
digitalWrite(5, HIGH); // turn the green LED on ,,,,,,,,,,,,,,,,,,,,,,, HIGH led ON????

It’s quite an interesting challenge for a novice….
You need to look at the fundamentals differently…
The LEDs are not on or off at a specific ‘time’, but you need to account for the ‘edges’, the ‘change of state’.

Look at the diagram more closely, and consider the ‘constant timebase’ of the edges, then consider what you need to keep track of.

In it simplest understanding, the green simply goes on and off with every clock edge.
Now count the edges to determine the red state…

Ok, with that understood, and everything working normally - the red has no dependence on the green, it’s only looking at the clocking edges.

You can simply stop blinking the green when the button is pressed.

Your next challenge is when to re-enable the green blink..! Asynchronously, or on the next edge, or dependent,on the red state… then carry on.

Unless they are wired differently, the comments are wrong.

sorry for comments i took them copy paste didnt read the mistake

it was copy paste mistake sorry

Hello andreas_hacked
It seems to be a XY problem.
In which cases the Arduino will be a either a consumer, producer or both for the LED "data"s?
Provide more details of your project.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

@andreas_hacked
karma for the timing diagram.
But a question:
if I just expand / repeat your diagram there will be a long sequence of green LED (V1). Is this really you want to get?
If I add one additional low phase (V2), the pulses stay in sync.

What do your really want to get?

In case of V2 my proposal is a state machine with 6 states (A-F) and a flag if the green LED should be on or not.

edit:
example for V2: [url]https://wokwi.com/projects/332339849373680212[/url]

// https://forum.arduino.cc/t/2-leds-on-off-and-push-button-to-turn-off-one-of-them/994227/10
// two LEDs blinking a pattern in sync
// one button

constexpr byte redPin = 4;   // GPIO for red LED
constexpr byte greenPin = 5; // GPIO for green LED
constexpr byte buttonPin = 6; // GPIO for the button
constexpr uint16_t interval = 1000; // interval in milliseconds


bool greenActive = true;    // should the green LED blink


/*
// V1
constexpr byte pattern[]
{
  0b01,   // red off, green green on
  0b10,   // red on, green off
  0b01,
  0b00,
  0b11,
};
*/
// V2
constexpr byte pattern[]
{
  0b01,   // red off, green green on
  0b10,   // red on, green off
  0b01,
  0b00,
  0b11,
  0b00, 
};
constexpr size_t noOfPattern = sizeof(pattern) / sizeof(pattern[0]);

void doFSM()
{
  static uint32_t previousMillis = 0;
  static byte blinkState = 0;
  uint32_t currentMillis = millis();
  if (currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    if (pattern[blinkState] & 0b01 && greenActive ) 
      digitalWrite(greenPin, HIGH);
    else 
      digitalWrite(greenPin, LOW);
    if (pattern[blinkState] & 0b10) 
      digitalWrite(redPin, HIGH);
    else 
      digitalWrite(redPin, LOW);
    blinkState++;
    if (blinkState >= noOfPattern) blinkState = 0; // rollover
  }
}

// this is based on "debounce" example
// just uses invers logic and some static/local variables
void readButton()
{
  static byte lastButtonState = HIGH;
  static uint32_t lastDebounceTime = 0;
  constexpr byte debounceDelay = 50;
  static byte buttonState;             // the current reading from the input pin
  byte reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == LOW) {
        greenActive = !greenActive;
        Serial.print(F("green will become ")); Serial.println(greenActive);
      }
    }
  }
  lastButtonState = reading;
}

void setup() {
  Serial.begin(115200);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  doFSM();
  readButton();
}
1 Like

We're still waiting to see your sketch.

1 Like
// 1 tick = 1/1024 second that means 0,00097sec near 1 msec so if I need 15.000 tick in a pulse means 12000 milli seconds
// the setup function runs once when you press reset or power the board
void setup() {
  
int inPin= 6;//push button
  // initialize digital pin LEDs 4 for red and 5 for green as an output.
  pinMode(4, OUTPUT); // consider pin 4 will be red led 
  pinMode(5, OUTPUT); // consider pin 5 will be green led 
  pinMode(inPin, INPUT);
      
}
int TLR=0;        //Times that Loop will be Reapeted 
// the loop function runs for once only!

void loop() {

 
  while(TLR<1)
  {
  digitalWrite(5, HIGH);             // turn the green LED on 
  delay(2000);                      // wait for 15 sec
  digitalWrite(5, LOW);              // turn the green LED off
  digitalWrite(4, HIGH);             // turn the red LED on 
  delay(2000);                      // wait for 15 sec
  digitalWrite(4, LOW);              // turn the red LED off
  digitalWrite(5, HIGH);             // turn the green LED on 
  delay(2000);                      // wait for 15 sec
  digitalWrite(5, LOW);              // turn the green LED off
  delay(2000);                      // wait for 15 sec
  digitalWrite(4, HIGH);             // turn the red LED on 
  digitalWrite(5, HIGH);             // turn the green LED on
  delay(2000);                      // wait for 15 sec
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  TLR=TLR+1;
  }
}

thats the problem : i) Program the LED (1,1) (Red diagram) and the Led (1,5) (green diagram) of the development to implement 5 complete cycles of the following sequence. Consider that the time that each green pulse is high, in the following waveforms is 15,000 clock cycles.
** idk what is (1,1 and 1,5) i just pass it...
ii) ii) Modify your program so that the green pulse does not flash when you press the A development switch

1 Like

i replace 15 to 2 , so I can test it faster... Now im gonna try with push button (i had h/w and didnt have time to try to fix it)

Thanks for reply <3 , thats a lot of code , I cant expand the diagram

Oh, I see. School assignment. Well it would be a lot quicker if you just posted the assignment.

im new ...and i apologize :frowning: ty a lot for ur time <3

But now that "15,000 clock cycles" has been invoked, all bets are off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.