Hello,
Im am very new to arduino and code. For a first project, I am trying to make three buttons simulate keyboard inputs and also light up, each in a different way. I can make the buttons or the leds work but I can not get them to work together. I have tried various combinations of code including serial.print, fallingedge, and others. I cant figure out what I am doing wrong and would greatly appreciate some direction.
I have three buttons. Button 0 should simulate pressing shift + letter a and loop through all colors via led. Button 1 should simulate pressing shift + letter b and cycle from white to blue in led or glow solid blue (which I was going to accomplish by supplying direct power to the blue led), and Button 2 should simulate pressing shift + letter b and glow white.
Here is the button code I am uising:
#include <Bounce.h>
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 ms debounce time is appropriate
Bounce button2 = Bounce(2, 10); // for most mechanical pushbuttons
// if a button is too "sensitive"
// you can increase this time.
// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;
// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds
void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip.
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
}
void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
// Check each button for "falling" edge.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_SHIFT);
Keyboard.set_key1(KEY_A);
Keyboard.send_now(); // send the button press
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now(); // send the button release
}
if (button1.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_SHIFT);
Keyboard.set_key1(KEY_B);
Keyboard.send_now();
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
}
if (button2.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_SHIFT);
Keyboard.set_key1(KEY_C);
Keyboard.send_now();
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
}
// Cycle color from red through to green
// (In this loop we move from 100% red, 0% green to 0% red, 100% green)
for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
redIntensity = 255-greenIntensity;
analogWrite(GREEN_LED_PIN, greenIntensity);
analogWrite(RED_LED_PIN, redIntensity);
delay(DISPLAY_TIME);
}
// Cycle color from green through to blue
// (In this loop we move from 100% green, 0% blue to 0% green, 100% blue)
for (blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5) {
greenIntensity = 255-blueIntensity;
analogWrite(BLUE_LED_PIN, blueIntensity);
analogWrite(GREEN_LED_PIN, greenIntensity);
delay(DISPLAY_TIME);
}
// Cycle cycle from blue through to red
// (In this loop we move from 100% blue, 0% red to 0% blue, 100% red)
for (redIntensity = 0; redIntensity <= 255; redIntensity+=5) {
blueIntensity = 255-redIntensity;
analogWrite(RED_LED_PIN, redIntensity);
analogWrite(BLUE_LED_PIN, blueIntensity);
delay(DISPLAY_TIME);
}
}
I have used many variations. this one operates the color fade correctly but the buttons dont "print". When i remove the color code the buttons print correctly.
Thanks
Robert