Go Down

Topic: 3x3 Array with button to change lighting mode (Read 167 times) previous topic - next topic

Matchlesmage0

I have a 3x3 array of LEDs and I hooked up a momentary switch to my Arduino to have it cycle the lighting modes with a press of the button my problem is that when I press it, it does not change unless it is at a certain time and I'm too new to fix the code on my own I have attached the file.

I have all of the LEDs connected to the 5v rail with all of their cathodes going on pin 3-10 with the momentary switch properly connected to pin 11 with a pull-up resistor

I'm also too young and Autistic to write things properly fix.

septillion

#1
Jul 05, 2018, 12:20 pm Last Edit: Jul 05, 2018, 12:21 pm by septillion
Code as it should have been posted:
Code: [Select]
int button_mode = 1;

int loopselector = 1;

void setup() {
  Serial.begin(9600);
  pinMode(11,INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(12, INPUT);
}

void loop() {
if ((digitalRead(11) == LOW) && (button_mode == 1))
  {
  button_mode = 0;
  Serial.println("Button has been pressed.");
  loopselector += 1;
  }
else if ((digitalRead(11) == HIGH) && (button_mode == 0))
  {
  button_mode = 1; // Button is released.
  Serial.println("Button has been released");
  }

 if (loopselector == 5) {
      loopselector = 1;
 }
 if (loopselector == 1) {
  //Pattern1
  reset();
  digitalWrite(2, LOW);
  delay(100);
  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  delay(100);
  digitalWrite(2, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(6, LOW);
  digitalWrite(4, LOW);
  delay(100);
  digitalWrite(3, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(9, LOW);
  delay(100);
  digitalWrite(8, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(10, LOW);
  delay(100);
  digitalWrite(7, HIGH);
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(10, HIGH);
 }
 if (loopselector == 2) {
  //Pattern2
  reset();
  digitalWrite(3, LOW);
  delay(500);
  digitalWrite(3, HIGH);
  delay(500);
 }
 if (loopselector == 3) {
  //Pattern3
  reset();
  digitalWrite(4, LOW);
  delay(500);
  digitalWrite(4, HIGH);
  delay(500);
 }
 if (loopselector == 4) {
  //Patern4
  reset();
  digitalWrite(5, LOW);
  delay(500);
  digitalWrite(5, HIGH);
  delay(500);
 } 

void reset()
{
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
}


Yep, that's the classic mistake of using delay(). A delay() blocks EVERYTHING. I'm still in favor of making it mandatory for every Howto on Arduino to have the second chapter "And now NEVER use delay() again!".

millis() is your answer. Have a look at Blink without delay and Demonstration code for several things at the same time.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up