Hi folks,
I am trying out the adafruit Trinket 5v for the first time and try to program a simple LED pattern on two LEDS (red and green).
What it should do:
red LED should be on (steady)
on button switch it should blink, turn off and the green led should be on (steady)
Wiring seems to be fine. Attached the LEDs to the pins 0 and 1 and the switch to pin 2.
Sadly it is not doing what i think it should do and I am lost in Coding.
Would be grateful if someone would check my code . Thank you so much in advance for help and comments on the project.
/*
Button
Turns off a light emitting diode(LED) connected to digital pin 0,
when pressing a pushbutton attached to pin 2.
Turns on a greed LED intead attached to pin 1.
The circuit:
- LED attached from pin 3 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPinred = 0; // the number of the LED pin
const int ledPingreen = 1; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPingreen, OUTPUT);
// initialize the LED pin as an output:
pinMode(ledPinred, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Off State steady red lights-check if the pushbutton is off. If it is, the buttonState is LOW:
if (buttonState == LOW) {
// turn redLED on:
digitalWrite(ledPinred, HIGH);
} else {
// turn LED off:
digitalWrite(ledPinred, LOW);
}
// On-State- check if the pushbutton is pressed. If it is, the buttonState is HIGH: (red blinks, green turns on and stays on)
if (buttonState == HIGH) {
// red LED blinks
digitalWrite(ledPinred, HIGH); // turn the led on
delay(1000); // wait for 1 second
digitalWrite(ledPinred, LOW); // turn the led off
delay(1000); // wait for a second
digitalWrite(ledPinred, LOW);
// turn greenLED on:
digitalWrite(ledPingreen, HIGH);
}
}