RGB LED with sound sensors

What's up team,

I am new to arduino and having trouble writing code for a project. My goal is to have a RGB LED that turns on when I make a sound and stays on until I make another sound and then switches to the next color. This pattern will continue until I run through all seven colors where it will then turn off after the 7th color. It is supposed to be a nightlight that turns on with a snap and switches colors with a snap.

this is my code so far, but I am pretty sure I am using the IF and IF ELSE statements incorrectly.

int soundSensor = 2;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup() {
// put your setup code here, to run once:

pinMode (soundSensor, INPUT);
pinMode (redPin, OUTPUT);
pinMode (greenPin, OUTPUT);
pinMode (bluePin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

int statusSensor = digitalRead (soundSensor);

if (statusSensor == 1)
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
else if (statusSensor == 0)
{
else
{
setColor(0, 255, 0);
}
setColor(225, 225, 0);
}
else
{
setColor(80, 0, 80);
}
else
{
setColor(0, 225, 255);
}
}
else
{
setColor(0, 0, 225);
}
else
{

void setColor(int red, int green, int blue)
{
#ifdef COMMON_CATHODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

thanks for the help

Yeah, I would say you should probably go work your way through a basic C++ tutorial and learn a little about if statements. When programming, you can never just guess at the syntax. You really do have to take the time to learn it first. And you are WAY off at the moment.

Alright I tried to learn more about IF statements but I am still really confused how to properly nest them. If I could see some code that uses them in the same way I am trying to in my project, it would be really helpful.

thanks

It works like this.

if(some condition) {
     // some block of code
}
 
else {
  // another block of code
}

The thing you have to understand is the braces. This is why it is important to format your code. When you format your code right you can see what is or isn't right with a series of if statements.

In an if statement, if the thing in the parenthesis is true then all the stuff between the { and the } that matches it will be run. If the stuff in the parenthesis is false then the whole block of code between the { and } is skipped and the else part is run instead. The braces that delineate the blocks of code are really important and if you line them up in matching sets (control - T in the IDE will do it for you) then you can see the logic pretty easy.

Now the stuff in one of those sets of braces can be more if statements. And there you will see how they look when properly formatted.

  if(something){
    if(somethingElse){
      // block of code 1
    }
    else {
      // block of code 2
    }
  }
  else {
    / block of code 3
  }

here if something is true then we go inside its braces and check somethingElse and run block 1 if it is true and block 2 if not.

We run block 3 if something is false and in that case we never look at somethingElse.

The else part can also lead to another if.

if(thingOne){
    // block 1
  }
  else if(thingTwo){
    // block 2
  }
  else if(thingThree){
    //block 3
  }
  else {
    // block 4
  }

and the first one that is true will run but nothing else. If thingOne is true then block 1 runs but we would never check thingTwo or thingThree. The else part block 4 runs if none of them are true.

Using this isn't a matter of programming. That part is actually dirt simple. You have to be able to write this all out in regular prose or pseudo-code or something before you can make it work. Once you can define the logic unambiguously then you can simply read the language of that logic directly into the language of code.

Awesome this is super helpful. The only thing I am still struggling with is that my condition for turning on the RGB LED, changing the color, and turning off the RGB LED all have the same condition which is (statusSensor == 0). So how do I use multiple IF statements if they all have the same condition?

thanks so much

You could set a variable to change every time you switch colors. So it would be (statusSensor == 0 && color == 1), then (statusSensor == 0 && color == 2), etc.

Every time the color went on, the variable could be incremented by 1, then you could use modulo arithmetic to loop through the numbers.