New to arduino. Simple starter problem

So I'm pretty decent with python. I bought some Arduino kits at least a year ago and I've been meaning to get around to messing with them some day. I injured my leg and am out of work so here's that day. Yay!! Anyway I set up my breadboard with 4 colored LED's. Red, green, yellow, and blue. Another RGB led and a button. The program I wrote is rotating from light to another with a short delay. Red-hold-green-hold and so on. When I press the button I want it to stop on the current light and simultaneously light the RGB led to that color. But without the button being pressed it should stay off. What is happening instead is when I press the button every time it lights the yellow led, turns the RGB to red and prints "RED" on the screen which was something I added to help with debugging. This c style programming language is a little bit of a learning curve for me. Cant stand all these curly braces and camel case convention haha. But seriously I'm just not really sure what I'm doing wrong. I feel like the whole program can be written better. Idk lemme know what you think.

// WHEN THE BUTTON IS PRESSED IT WILL PAUSE AND TURN THE RGB LED TO THE COLOR OF THE LED THAT IS CURRENTLY LIT

const int redledPin = 8;      // the pin that the RED LED is on
const int greenledPin = 9;      // the pin that the GREEN LED is on
const int yellowledPin = 10;     // the pin that the YELLOW LED is on
const int blueledPin = 11;      // the pin that the BLUE LED is on

const int rgbledRPin = 3;     // the pin that the RGB LED - RED is on
const int rgbledGPin = 5;     // the pin that the RGB LED - GREEN is on
const int rgbledBPin = 6;     // the pin that the RGB LED - BLUE is on

const int buttonPin = 2;      // the pin the button is connected to
const int delayTime = 250;    // delayed time in milliseconds between light change
int buttonState = 0;        // button state ON or OFF
int RGY = 8;                // 8=RED 9=GREEN 10=YELLOW (starting at red)
bool fowardBackward = 1;     // 0 = foward  1 = backward

 
void setup() {
  Serial.begin(9600);
  // INDIVIDUAL LED'S PINS
  pinMode(redledPin, OUTPUT);  // initialize the RED LED pin as an output
  pinMode(greenledPin, OUTPUT);  // initialize the GREEN LED pin as an output
  pinMode(yellowledPin, OUTPUT);  // initialize the YELLOW LED pin as an output
  pinMode(blueledPin, OUTPUT);    // initialize the BLU LED pin as an output
  
  // RGB LED PINS
  pinMode(rgbledRPin, OUTPUT);  // initialize the RGB LED (RED) pin as an output
  pinMode(rgbledGPin, OUTPUT);  // initialize the RGB LED (GREEN) pin as an output
  pinMode(rgbledBPin, OUTPUT);  // initialize the RGB LED (BLUE) pin as an output

  // BUTTON PIN
  pinMode(buttonPin, INPUT);      // initialize button pin as an input
}
 
void loop() {
  //buttonState = digitalRead(buttonPin);   // check if button is pressed

  if(digitalRead(buttonPin) == HIGH)                 // check if button is pressed
    {
      if(RGY = 8) {
        Serial.println("RED");
        analogWrite(rgbledRPin, 255);
        analogWrite(rgbledGPin, 0);
        analogWrite(rgbledBPin, 0);
        }
      else if(RGY = 9) {
        Serial.println("GREEN");
        analogWrite(rgbledGPin, 255);
        analogWrite(rgbledRPin, 0);
        analogWrite(rgbledBPin, 0);
        }
      else if(RGY = 10) {
        Serial.println("YELLOW");
        analogWrite(rgbledRPin, 255);
        analogWrite(rgbledGPin, 255);
        analogWrite(rgbledBPin, 0);
        }
      else if(RGY = 11) {
        Serial.println("BLUE");
        analogWrite(rgbledBPin, 255);
        analogWrite(rgbledGPin, 0);
        analogWrite(rgbledRPin, 0);
        }
    }

  digitalWrite(RGY, HIGH);    // turn ON currently selected light
  delay(delayTime);                 // pause .5 seconds
  digitalWrite(RGY, LOW);    // turn OFF currently selected light

  if(!digitalRead(buttonPin)){
        analogWrite(rgbledBPin, 0);
        analogWrite(rgbledGPin, 0);
        analogWrite(rgbledRPin, 0);
    if(RGY < 11)          // move to next light
      {RGY = RGY + 1;}
    else
      {RGY = 8;}
  }
}
![arduino1|375x500](upload://jVRPoPn2HT4xr2XZwyOLbwferAe.jpeg)
![arduino2|375x500](upload://4jOMcDuWIKIwgMlb4KOA0jGqwgF.jpeg)

type or paste code here

Your two images got buried in the code section.

Not needed. Different variables only need to be different and in the correct scope.

if(RGY = 8) {

This makes RGY the value 8, and therefore "RED". You probably wanted (throughout your sketch):

if(RGY == 8) {

"Is equal to"

... I just tested... it should work for you,too.

    if(RGY < 11)          // move to next light
      {RGY = RGY + 1;}
    else
      {RGY = 8;}

Braces not needed...

    if(RGY < 11)          // move to next light
      RGY = RGY + 1;
    else
      RGY = 8;
2 Likes

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW and may need to be debounced (e.g. delay (20):wink:

Really?

You confused = (assignment) with == (equality). These symbols have exactly the same meanings in both Python and C.

look this over

// WHEN THE BUTTON IS PRESSED IT WILL PAUSE AND TURN THE RGB LED TO THE COLOR OF THE LED THAT IS CURRENTLY LIT

const int redledPin    = 8;      // the pin that the RED LED is on
const int greenledPin  = 9;      // the pin that the GREEN LED is on
const int yellowledPin = 10;     // the pin that the YELLOW LED is on
const int blueledPin   = 11;      // the pin that the BLUE LED is on

const int rgbledRPin   = 3;     // the pin that the RGB LED - RED is on
const int rgbledGPin   = 5;     // the pin that the RGB LED - GREEN is on
const int rgbledBPin   = 6;     // the pin that the RGB LED - BLUE is on

const int buttonPin    = A2;    // 2; 
const int delayTime    = 250;    // delayed time in milliseconds between light change

int RGY = 8;                // 8=RED 9=GREEN 10=YELLOW (starting at red);

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (redledPin,    OUTPUT);
    pinMode (greenledPin,  OUTPUT);
    pinMode (yellowledPin, OUTPUT);
    pinMode (blueledPin,   OUTPUT);

    // RGB LED PINS
    pinMode (rgbledRPin,   OUTPUT);
    pinMode (rgbledGPin,   OUTPUT);
    pinMode (rgbledBPin,   OUTPUT);

    pinMode (buttonPin,    INPUT_PULLUP);
}

// -----------------------------------------------------------------------------
void loop ()
{
    digitalWrite (RGY, HIGH);    // turn ON currently selected light
    delay (delayTime);           // pause .25 seconds
    digitalWrite (RGY, LOW);     // turn OFF currently selected light

    if (LOW == digitalRead(buttonPin)) {    // pressed
        if (RGY == 8) {
            Serial.println ("RED");
            analogWrite (rgbledRPin, 255);
            analogWrite (rgbledGPin, 0);
            analogWrite (rgbledBPin, 0);
        }
        else if (RGY == 9) {
            Serial.println ("GREEN");
            analogWrite (rgbledGPin, 255);
            analogWrite (rgbledRPin, 0);
            analogWrite (rgbledBPin, 0);
        }
        else if (RGY == 10) {
            Serial.println ("YELLOW");
            analogWrite (rgbledRPin, 255);
            analogWrite (rgbledGPin, 255);
            analogWrite (rgbledBPin, 0);
        }
        else if (RGY == 11) {
            Serial.println ("BLUE");
            analogWrite (rgbledBPin, 255);
            analogWrite (rgbledGPin, 0);
            analogWrite (rgbledRPin, 0);
        }
    }

    else  {
        analogWrite (rgbledBPin, 0);
        analogWrite (rgbledGPin, 0);
        analogWrite (rgbledRPin, 0);
        if (RGY < 11)          // move to next light
            RGY = RGY + 1;
        else
            RGY = 8;
    }
}

Fair. I'm rusty. Haven't studied in months close to a year tbh. I try but have a very difficult time practicing after full time job and everything else in life.

Thanks guys. Great help! One other thing. What are your preferred IDE's? Or do you stick with the arduino ide?

what are your other options? platformIo?

i use my own editor (vim), use the IDE to compile, download and the Serial monitor for debugging/diagnostics

Why would you write the if statement this way
if (LOW == digitalRead(buttonPin)) { // pressed
with the low before the variable?
Seams more intuitive to put the variable first.

1 Like

see Yoda Coding

1 Like

I stick with the Arduino IDE. I still haven't tried V2.x, I have been clinging to V1.8.19! Eventually I'm sure I'll have to upgrade but for now I still enjoy using the slightly retro old version.

Yes, I do know what I'm missing. I am quite used to more advanced IDE like IntelliJ (for SCALA) and PyCharm (neither for Arduino, of course).

1 Like

FWIW 99.9943 percent of programmers do it in the natural way you might have learned in maths.

A fair percentage of us find that notation to be more confusing than helpful, it just goes against too much brain training.

a7

1 Like

I'm staying with 1.8.19 until it just doesn't work anymore.

If you make a mistake and use a single = an assignment will be done, which is probably not intended. In the old days the error would not be noticed. Putting the constant first will cause a compile error which is obvious. Now most compilers will give a warning (if you look at them) so it isn't as much of a problem. I have changed from the constant first style over the last few decades.

1 Like

… or something even a bit younger until this and other things force me to upgrade my OS, which will mean buying a new computer, which will also mean many applications I use will no longer run.

a7

I appreciate a good ide like pycharm that helps speed up your process and catch mistakes too. Some say that it will prevent you from learning but i don't think so. Typically it doesn't fix your mistakes it just tells you when it sees one and what might be the right fix. No matter what you have to use your brain to code. It's not like the ide is going to do everything for you. If anything I think it helps you learn by pointing things out. I've learned a lot from pycharm.

That's what i was wondering if there are any other options. I know some ides work with many languages so i thought some might even work for arduino. At the same time i know it's based off of C language. I don't know how much it differs. Idk is there anything designed for C that you can use for this?

Arduino is programmed in C++.

I use the Eclipse / Sloeber IDE.

1 Like

it's not about the language, does the IDE know how to download the code and support the Serial monitor

Ya well I was figuring using and IDE to write the code. Save the file then just open with arduino software for the upload. But come to think of it, it sounds like more hassle than its worth. Then again maybe not. Notepad++ or sublime would probably be a better environment to write the code. Ill have to check out what gfvalvo mentioned. Eclipse/Sloeber