Cannot turn a pattern off

bool button1 = false;
bool button2 = false;
bool button3 = false;

const int redPin = 2;
const int bluePin = 1;
const int greenPin = 0;

int ColourValueRed;
int ColourValueGreen;
int ColourValueBlue;
void setup()
{
  pinMode(13, OUTPUT);	//LED group 1
  pinMode(12, OUTPUT);	//LED group 2
  pinMode(11, OUTPUT);	//LED group 3
  pinMode(10, OUTPUT);	//LED group 4
  pinMode(9, OUTPUT);	//LED group 5
  pinMode(8, OUTPUT);	//LED group 6
  pinMode(7, OUTPUT);	//LED group 7
  pinMode(6, OUTPUT);	//LED group 8
  pinMode(5, INPUT);	// Button 1
  pinMode(4, INPUT);	// Button 2
  pinMode(3, INPUT);	// Button 3
  pinMode(2, OUTPUT);	// Red
  pinMode(1, OUTPUT);	// Blue
  pinMode(0, OUTPUT);	// Green
  pinMode(A0, INPUT);	// Potentiometer
}

void loop()
{
  {
  if (digitalRead(5) == HIGH)
  {
    if(button1 == false)
    {
    button1 = true;
      while (button1 = true)
      {
      digitalWrite(13, HIGH);
        digitalWrite(11, HIGH);
        digitalWrite(9, HIGH);
        digitalWrite(10, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        delay(250);
        digitalWrite(13, LOW);
        digitalWrite(11, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(12, HIGH);
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        delay(250);
      }
    }
  }
    if (button1 == true){
    button1 = false;
      while (button1 = false){
        digitalWrite(11, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(12, LOW);
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
      }      
    }
  }
}

Here is my code. I’m trying to turn my button off, but its not working. Can you please tell me how to improve this, I have absolutely no idea.
Thank you.

afficianado:       while (button1 = true)

Please study the difference between = (assignment operator) and == (comparison operator): https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/assignment/ https://www.arduino.cc/reference/en/language/structure/comparison-operators/equalto/ Same problem at line 65.

Also, you have nothing within the while loops to change the value of button1, so once you enter the loop, you will never leave.

[edit] It is also not a good idea to use D0 and D1 on an UNO, those are used by the serial communications on the USB. You can use the analog pins with the digitalRead and digitalWrite instructions, the same as the digital pins, just make sure to use the Ax notation when referring to them.

My code is a 3 button code that has a pattern. Each of my buttons have a separate button.

I can turn on one button once I start the code.
Once I turn the button off and press another button, that button doesn’t work. Each button works individually, if you get what I mean.
I am using TinkerCad as my Arduino so I have no access to more pins. I have to use pin 1, 0.
How can I improve my code to turn on buttons in the same simulation.

bool button1 = false;
bool button2 = false;
bool button3 = false;

const int redPin = 2;
const int bluePin = 1;
const int greenPin = 0;

int ColourValueRed;
int ColourValueGreen;
int ColourValueBlue;
void setup()
{
  pinMode(13, OUTPUT);	//LED group 1
  pinMode(12, OUTPUT);	//LED group 2
  pinMode(11, OUTPUT);	//LED group 3
  pinMode(10, OUTPUT);	//LED group 4
  pinMode(9, OUTPUT);	//LED group 5
  pinMode(8, OUTPUT);	//LED group 6
  pinMode(7, OUTPUT);	//LED group 7
  pinMode(6, OUTPUT);	//LED group 8
  pinMode(5, INPUT);	// Button 1
  pinMode(4, INPUT);	// Button 2
  pinMode(3, INPUT);	// Button 3
  pinMode(2, OUTPUT);	// Red
  pinMode(1, OUTPUT);	// Blue
  pinMode(0, OUTPUT);	// Green
  pinMode(A0, INPUT);	// Potentiometer
}

void loop()
{	
  {
   int rgbvalue = map(analogRead(A0), 0, 1024, 0, 255);
    
    if (rgbvalue <=42.5)
    {
      ColourValueRed = 255;
      ColourValueGreen = rgbvalue * 6;
      ColourValueBlue = 0;
    }
    
    if ((rgbvalue > 42.5) && (rgbvalue <= 85))
    {
      ColourValueRed = 255 - (rgbvalue - 43) * 6;
      ColourValueGreen = 225;
      ColourValueBlue = 0;
    }
    if ((rgbvalue > 85) && (rgbvalue <= 127.5)) 
    {
		ColourValueRed = 0;
		ColourValueGreen = 255;
		ColourValueBlue = (rgbvalue - 85) * 6;
    }
        if ((rgbvalue > 127.5) && (rgbvalue <= 170)) 
        {
		ColourValueRed = 0;
		ColourValueGreen = 255 - (rgbvalue - 127.5) * 6;
		ColourValueBlue = 0;
	}
   
	if ((rgbvalue > 170) && (rgbvalue <= 212.5)) 
    {
		ColourValueRed = (rgbvalue - 170) * 6;
		ColourValueGreen = 0;
		ColourValueBlue = 0;
	}
	
    if ((rgbvalue > 212.5) && (rgbvalue <= 255)) {
		ColourValueRed = 0;
		ColourValueGreen = 0;
		ColourValueBlue = 255 - (rgbvalue - 212.5) * 6;
	}
    analogWrite(2, ColourValueRed);
	analogWrite(1, ColourValueBlue);
	analogWrite(0, ColourValueGreen);
  }
  {
  if (digitalRead(5) == HIGH)
    {
    button1 = !button1;
    while(1>0)
      {
        digitalWrite(13, button1);
        digitalWrite(11, button1);
        digitalWrite(9, button1);
        digitalWrite(10, button1);
        digitalWrite(12, LOW);
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        delay(250);
        digitalWrite(13, LOW);
        digitalWrite(11, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(12, button1);
        digitalWrite(8, button1);
        digitalWrite(7, button1);
        digitalWrite(6, button1);
        delay(250);
     	 {
 			if (digitalRead(5) == HIGH)
    		{
    			button1 = !button1;
  			}
     	 }
      }
  	}
  }
  if(digitalRead(4) == HIGH)
  {
   button2 = !button2;
   while(1>0)
   {
     digitalWrite(6,button2);
     digitalWrite(7,button2);
     digitalWrite(8,button2);
     digitalWrite(9,button2);
     digitalWrite(10,button2);
     digitalWrite(11,button2);
     digitalWrite(12,LOW);
     digitalWrite(13,LOW);
     delay(250);
     digitalWrite(6,LOW);
     digitalWrite(7,LOW);
     digitalWrite(8,LOW);
     digitalWrite(9,LOW);
     digitalWrite(10,LOW);
     digitalWrite(11,LOW);
     digitalWrite(12,button2);
     digitalWrite(13,button2);
     delay(250);
     {
 			if (digitalRead(4) == HIGH)
    		{
    			button2 = !button2;
  			}
     	 }
   }
  }
  if(digitalRead(3) == HIGH)
  {
   button3 = !button3;
   while(1>0)
   {
     digitalWrite(6,button3);
     digitalWrite(7,button3);
     digitalWrite(8,button3);
     digitalWrite(9,button3);
     digitalWrite(10,button3);
     digitalWrite(11,button3);
     digitalWrite(12,button3);
     digitalWrite(13,button3);
     {
 			if (digitalRead(3) == HIGH)
    		{
    			button3 = !button3;
  			}
     	 }
     }
  }
}

pert: It is also not a good idea to use D0 and D1 on an UNO, those are used by the serial communications on the USB. You can use the analog pins with the digitalRead and digitalWrite instructions, the same as the digital pins, just make sure to use the Ax notation when referring to them.

Thanks for the help with the loops. I have done my other two buttons. What do you mean by the Ax notation when using the analog pin.

Let's say you wanted to set digital pin 2 HIGH. The code would look like this:

digitalWrite(2, HIGH);

Now let's say you wanted to set pin A2 HIGH. The code would look like this:

digitalWrite(A2, HIGH);

Hello,

can you upload a screenshot of your tinkercad and explain a bit what your project is supposed to do?

It looks a bit like you're trying to use your inputs as outputs (setting button1 = !button1 etc).

Using pins 0 and 1 does tend to cause problems, as you obviously know - could you shift these pins to two of the analogue pins and just use them as digital ones? You only seem to be using pin A0, so the others are free?

:)

bool button1 = false;
bool button2 = false;
bool button3 = false;

const int redPin = 2;
const int bluePin = 1;
const int greenPin = 0;

int ColourValueRed;
int ColourValueGreen;
int ColourValueBlue;
void setup()
{
  pinMode(13, OUTPUT);	//LED group 1
  pinMode(12, OUTPUT);	//LED group 2
  pinMode(11, OUTPUT);	//LED group 3
  pinMode(10, OUTPUT);	//LED group 4
  pinMode(9, OUTPUT);	//LED group 5
  pinMode(8, OUTPUT);	//LED group 6
  pinMode(7, OUTPUT);	//LED group 7
  pinMode(6, OUTPUT);	//LED group 8
  pinMode(5, INPUT);	// Button 1
  pinMode(4, INPUT);	// Button 2
  pinMode(3, INPUT);	// Button 3
  pinMode(A2, OUTPUT);	// Blue
  pinMode(A1, OUTPUT);	// Green
  pinMode(A3, OUTPUT);	// Red
  pinMode(A0, INPUT);	// Potentiometer
}

void loop()
{/*
  {
   int rgbvalue = map(analogRead(A0), 0, 1024, 0, 255);
    
    if (rgbvalue <=42.5)
    {
      ColourValueRed = 255;
      ColourValueGreen = rgbvalue * 6;
      ColourValueBlue = 0;
    }
    
    if ((rgbvalue > 42.5) && (rgbvalue <= 85))
    {
      ColourValueRed = 255 - (rgbvalue - 43) * 6;
      ColourValueGreen = 225;
      ColourValueBlue = 0;
    }
    if ((rgbvalue > 85) && (rgbvalue <= 127.5)) 
    {
		ColourValueRed = 0;
		ColourValueGreen = 255;
		ColourValueBlue = (rgbvalue - 85) * 6;
    }
        if ((rgbvalue > 127.5) && (rgbvalue <= 170)) 
        {
		ColourValueRed = 0;
		ColourValueGreen = 255 - (rgbvalue - 127.5) * 6;
		ColourValueBlue = 0;
	}
   
	if ((rgbvalue > 170) && (rgbvalue <= 212.5)) 
    {
		ColourValueRed = (rgbvalue - 170) * 6;
		ColourValueGreen = 0;
		ColourValueBlue = 0;
	}
	
    if ((rgbvalue > 212.5) && (rgbvalue <= 255)) {
		ColourValueRed = 0;
		ColourValueGreen = 0;
		ColourValueBlue = 255 - (rgbvalue - 212.5) * 6;
	}
    analogWrite(A3, ColourValueBlue);
	analogWrite(A2, ColourValueGreen);
	analogWrite(A1, ColourValueRed);
  }*/
  {
  if (digitalRead(5) == HIGH)
    {
    button1 = !button1;
    while(1>0)
      {
        digitalWrite(13, button1);
        digitalWrite(11, button1);
        digitalWrite(9, button1);
        digitalWrite(10, button1);
        digitalWrite(12, LOW);
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        delay(250);
        digitalWrite(13, LOW);
        digitalWrite(11, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(12, button1);
        digitalWrite(8, button1);
        digitalWrite(7, button1);
        digitalWrite(6, button1);
        delay(250);
     	 {
 			if (digitalRead(5) == HIGH)
    		{
    			button1 = !button1;
  			}
     	 }
      }
  	}
  }
  if(digitalRead(4) == HIGH)
  {
   button2 = !button2;
   while(1>0)
   {
     digitalWrite(6,button2);
     digitalWrite(7,button2);
     digitalWrite(8,button2);
     digitalWrite(9,button2);
     digitalWrite(10,button2);
     digitalWrite(11,button2);
     digitalWrite(12,LOW);
     digitalWrite(13,LOW);
     delay(250);
     digitalWrite(6,LOW);
     digitalWrite(7,LOW);
     digitalWrite(8,LOW);
     digitalWrite(9,LOW);
     digitalWrite(10,LOW);
     digitalWrite(11,LOW);
     digitalWrite(12,button2);
     digitalWrite(13,button2);
     delay(250);
     {
 			if (digitalRead(4) == HIGH)
    		{
    			button2 = !button2;
  			}
     	 }
   }
  }
  if(digitalRead(3) == HIGH)
  {
   button3 = !button3;
   while(1>0)
   {
     digitalWrite(6,button3);
     digitalWrite(7,button3);
     digitalWrite(8,button3);
     digitalWrite(9,button3);
     digitalWrite(10,button3);
     digitalWrite(11,button3);
     digitalWrite(12,button3);
     digitalWrite(13,button3);
     {
 			if (digitalRead(3) == HIGH)
    		{
    			button3 = !button3;
  			}
     	 }
     }
  }
}

I have since updated the code, but not by much.
The first button has a pattern where every second light is turned on.
The second button has a pattern where the inside lights turn on, then the outer lights turn on etc.
The third button turns everything on or off.
I have a potentiometer for the RGB but I have commented it out for now.

Thank you for the help?


I see 8 series resistors for 16 LEDs. Are pairs of LEDs wired in series? I cannot see from your messy diagram. A schematic would be better.

while(1>0)

You have a couple of loops like the above. Once they start, they can never stop, until you press reset.

Hi, Is this associated with this?

https://forum.arduino.cc/index.php?topic=631923.msg4278784#msg4278784

Tom... :)

 while(1>0)
      {
        digitalWrite(13, button1);
        digitalWrite(11, button1);
        digitalWrite(9, button1);
        digitalWrite(10, button1);
        digitalWrite(12, LOW);
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        delay(250);
        digitalWrite(13, LOW);
        digitalWrite(11, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(12, button1);
        digitalWrite(8, button1);
        digitalWrite(7, button1);
        digitalWrite(6, button1);
        delay(250);
       {
            if (digitalRead(5) == HIGH)
          {
              button1 = !button1;
           }
       }
      }
  1. Please format that mess. When you see real coders with all their blocks of code lined up all nice and neat, that isn't done just to make it pretty. It also makes it readable. ALl you have to do is press Control-T in the IDE and it will do it for you. There's no excuse to have braces wandering all over the page like this.

  2. while(1>0) is called an infinite loop. Once you enter that you never exit. It never runs any other part of your code. It is trapped in that loop forever.

Hi,

You code would be easier to read if you gave variable names to your pin numbers.
For example;

bool button1
int button1Pin = 5;

Then in void setup();

pinMode(button1Pin, INPUT);

Then in void loop();

button1 = digitalRead(button1Pin);

if (button1 == HIGH)

This will greatly improve the readability of you code and encourage more to help.

remove the

while (1 > 0)

As it just produces a loop you cannot get out.
You should be reading ALL your button inputs at the start of the loop, then do your if statements on the bool values of the respective button values.
Each time you loop the button values will then be updated.

Can you tell us your electronics, programming, arduino, hardware experience?

Thanks… Tom… :slight_smile:

You code would be easier to follow if you used comments to explain what you are doing.

@afficianado, please do not cross-post. Threads merged.

TomGeorge:
You should be reading ALL your button inputs at the start of the loop, then do your if statements on the bool values of the respective button values.
Each time you loop the button values will then be updated.

Can you tell us your electronics, programming, arduino, hardware experience?

Hi Tom,
Sorry to all for using two threads for one topic.
Can you please explain how to read all the button inputs first, then the bools?

As you may tell, I am just starting out in Arduino. Except for knowing some HTML, my code knowledge is pretty small.
Thank you

Hi, Post #11 shows you how to do this for button1, just do the same for 2 and 3.

Put this and lines for button 2 and 3

button1 = digitalRead(button1Pin);

as the very start of your void loop()'

Then use button1 , button2 and button3 values in any of your if statements.

ONLY read your buttons at the start of the loop.

Tom... :)