Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int r = 6; // select the output pin for the Red LED
- int g = 5; // select the output pin for the Green LED
- int b = 3; // select the output pin for the Blue LED
- int potPin = 4; // select the input pin for the potentiometer
- int buttonPin = 8; // select the input pin for the button
- int buttonState = 0; // current state of the button
- int buttonCounter = 0; // counter to prevent switching while button is pressed
- int val = 0; // variable to store the value coming from the sensor
- int mode = r; // mode for setting which colour to change, defaults to red
- void setup()
- {
- pinMode(r, OUTPUT);
- pinMode(g, OUTPUT);
- pinMode(b, OUTPUT);
- pinMode(buttonPin, INPUT);
- }
- void loop()
- {
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH && buttonCounter == 0)
- {
- buttonCounter++;
- if (mode == r)
- {
- mode = g;
- }
- else if (mode == g)
- {
- mode = b;
- }
- else //(mode == r)
- {
- mode = r;
- }
- }
- if (buttonState == LOW)
- {
- buttonCounter = 0;
- }
- val = analogRead(potPin); // read the value of the potentiometer
- val = map(val, 0, 1023, 0, 255); // convert analog input to 8 bits (0 to 255)
- setIntensity(mode, val); // and throw that value to setIntensity() to set the LEDs brightness
- }
- void setIntensity(int colour, int colourValue)
- {
- analogWrite(colour, colourValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement