Part of learning is growing neural connections. Take breaks, refuse to fixate for long periods, it will burn you out and keep you in a groove. Read until tired or stuck then go do something else, come back later and look again.
Don't dry out and start learning a lot of patience. Professionally, I've been wrong 30 times before lunch not counting the tiny stuff, or so the compiler told me. Attention to detail during debug; just 1 character wrong (tiny stuff) has to be fixed.
I kind of got pressured because I only have a spare 2 to 3 hours a day to tinker with the codes, and I am also dealing with 3d printing and assembling the model. I should slow down a bit lol
This should generate all the patterns you have described if called in the loop() every 50ms or faster.
However, it is very inefficient.
You still have change the led pin variables and the control variables for the buttons.
However, you should try to use a finite state machine type approach.
// Button 1 simple ON/OFF toggle
digitalWrite( pinB1_LED , patternB1_enable ? HIGH : LOW ) ;
// Button 2, first LED: 2Hz 10% mark to space ratio
digitalWrite( pinFirstLed , patternB2_enable && millis() % 500 < 50 ? HIGH : LOW ) ;
// Button 2, second LED: 1Hz 5% mark to space ratio
digitalWrite( pinSecondLed , patternB2_enable && millis() % 1000 < 50 ? HIGH : LOW ) ;
// Button 3, 1Hz 10% mark to space ratio
digitalWrite( pinB3_LED , patternB3_enable && millis() % 1000 < 100 ? HIGH : LOW ) ;
The only thing I still can't figure out is how do switch the LED 1 or LED 2 off via IR remote.(Because based from the sample code of the guide, the LED 1 and LED 2 switches on and blinks permanently, so how do I actually toggle them on or off using IR Remote.
void loop ()
{
// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();
// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop
In what part do I insert the command to toggle LED 1 or LED 2 to on or off?
Is it in this part?
if (irrecv.decode(&results))
{
unsigned int value = results.value;
switch (value)
{
case code1:
if (itsONled[1]) // if first led is on then
{
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = false; // and set its state as off
}
else // else if first led is off
{
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = true; // and set its state as on
}
break;
Let's go back to basics. This is two parts.
Part 1 is to determine which LED sequences the user has chosen via the remote control unit, and set status variables appropriately to store the user's choice(s).
Part 2 is to generate those LED sequences by reading the status variables and using timers etc.
These parts can even be so written that they can be tested independently.
Your latest code shows a mixture of switching leds and the corresponding status variables which will not work.
My suggestion is that just for now you start with part 1. Write a simple sketch which reads the IR receiver , maintains status variables to indicate the user's choices, and writes to the serial console something like:
Button1 pattern ON (or OFF depending on what the user has chosen)
Button2 pattern OFF (or ON depending on what the user has chosen)
Button3 pattern ON (or OFF depending on what the user has chosen)
Once you have got that far, you can add the code for part 2 to generate the patterns.
Simply so.
button 1 toggles a status variable which indicates the current state of button 1.
When the system starts, this status variable has the value false.
The first time it is pressed, it has the value true.
The next time it is pressed, it has the value false again. etc.
The Led pattern generator reads that status variable and acts accordingly.
If it finds the status variable has a value false, it does nothing.
If it finds it has a value true, it continues displaying the sequence for the led associated with button 1, depending on the time (milliseconds) into the cycle.
Basically you need to detect when the button becomes pressed rather than when it is pressed and depending on the current state of the system take the appropriate action
The function LEDPattern1() is defined outside of any function. Defined.
But the function runs inside of void loop() even to check the time and return.
// void loop() runs over and over, without blocking it runs very fast
// every function takes a tiny step even if just to check conditions and say No.
void loop()
{
LEDPattern1(); // only does anything when enabled
LEDPattern2(); // ditto
buttonRead(); // returns current button state
processButton(); // enable/disable led patterns according to button use
processSerial(); // change blink time while the sketch runs
}
void loop ()
{
// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();
// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop
toggleGreenLED () ; ---- is a function call needing a function defined outside of setup(), loop() or any other function.
void toggleGreenLED (void) // toggle flips the ON/OFF switch
{
byte state; // temporary variable only works inside of the function
state = digitalRead( greenLedPin );
digitalWrite( greenLedPin, !state ); // !state is logical NOT state
}