Buttons were pretty fun this week!
Here are the videos for exercises 1 and 2:
For the third one, I made a pretty fun RGB color mixer:
Code:
//LED pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
//button pins
const int redButton = 2;
const int greenButton = 3;
const int blueButton = 4;
//initialize brightness values to 0
int redVal = 0;
int greenVal = 0;
int blueVal = 0;
//button press boolean
int redPress = 1;
int greenPress = 1;
int bluePress = 1;
void setup()
{
//set LED pins to outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//set button pins to inputs
pinMode(redButton, INPUT);
pinMode(greenButton, INPUT);
pinMode(blueButton, INPUT);
//begin serial monitor
Serial.begin(9600);
}
void loop()
{
//test if any of the brightness values are at maximum
//if so, reset to 0
if (redVal >= 255) {
redVal = 0;
}
if (greenVal >= 255) {
greenVal = 0;
}
if (blueVal >= 255) {
blueVal = 0;
}
//set the LED colors to the current brightness values
setColor(redVal, greenVal, blueVal);
//check whether or not a button is pressed
redPress = digitalRead(redButton);
greenPress = digitalRead(greenButton);
bluePress = digitalRead(blueButton);
//if the a control button is pressed,
//increase the corresponding brightness value by 5
if (redPress == LOW) {
redVal = redVal + 5;
Serial.print("Red value is ");
Serial.print(redVal);
Serial.println();
}
if (greenPress == LOW) {
greenVal = greenVal + 5;
Serial.print("Green value is ");
Serial.print(greenVal);
Serial.println();
}
if (bluePress == LOW) {
blueVal = blueVal + 5;
Serial.print("Blue value is ");
Serial.print(blueVal);
Serial.println();
}
//delay to slow the fade and increase control
delay(100);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
