Arduino Using buttons to switch function

Feb 1, 2017 at 9:27am
Hi! I'm new to Arduino and I'm trying to create a program that has function according to its LED

So I have 2 buttons, 8 leds, 1 rgb. b1 is the function switcher and b2 is the function activator and here's the function.

Function 1: Turn on/off RED light
Function 2: Increase RED light brightness by 5; if already at 255, reset at 0
Function 3: Turn on/off GREEN light
Function 4: Increase GREEN light brightness by 5; if already at 255, reset at 0
Function 5: Turn on/off BLUE light
Function 6: Increase BLUE light brightness by 5; if already at 255, reset at 0
Function 7: Turn on ALL lights, all brightness to 255
Function 8: Turn off ALL lights, all brightness to 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  const int red = 5;


int b1, b2; //Buttons
int dir =0; //2 led goes to right
int light =11;
int func = 0;
boolean buttonValue = true;
boolean buttonState = true;



void setup() {
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(red, OUTPUT);
  for (int i =11; i<=18; i++) {
    pinMode(i, OUTPUT);
  }
  
}

/**
 * This is the function switcher so when I press b1 it changes the function and it lights up the led
 * Ex when I turn on device the led1 is already turned on led1 is the function 1 so when I press b2 the RGB turns on and goes to red color
 * when i press b1 again it goes to 2nd led then its function it increases the brightness of RED color
 */

void loop() {
  b1 = digitalRead(2);
  
  if (b1 == HIGH) {
    dir = 2;
    func++;
    if(func >=8)
    func = 0;
  } else {
    dir = 0; 
  }
   digitalWrite(light, LOW); 
  if(dir==2 && light < 18) { 
    light++; 
  } 
  digitalWrite(light, HIGH); 
  delay(200); 
} 


void function(int pin) {
  b2 = digitalRead(3);

  switch(func) {
    case 1:
    if (b2 == 0) {
      digitalWrite(5, HIGH);
    } else {
      digitalWrite(5, LOW);
    }    
  }
}



My problem is when I press b2 the RGB led doesn't turn on what's my problem here can anyone help me?
Last edited on Feb 1, 2017 at 9:36am
Feb 1, 2017 at 11:32am
closed account (48T7M4Gy)
https://www.arduino.cc/en/Tutorial/Button

There is enough complexity in you program and the Arduino setup that goes with it such that I would recommend strongly that you get each one of the functions 1-8 to separately work. Isolate and fixup any problems then and only then move on to the next function.

You might have something as simple as a hardware problem/loose connection and not just a programming error.

You will save a lot of time and heartbreak by doing unit testing whether you're a new learner or expert.

You also need to document your code properly showing exactly what each function is supposed to do and have a clear description of how it is done eg at the moment you don't state how dimming is achieved. You are p..ing into the wind with an Arduino if you just write line after line and hope you catch a solution. Arduino's are easy if you take a disciplined approach :)

Topic archived. No new replies allowed.