Beginner Adafruit Trinket 5v LED Program help

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);






  } 
}

Here is a description on what happens:

Red LED is on
on Button press both LED turn on
on another button press the red led turn off and on again after a second

Show us your circuit. Once green turns on, it never turns off.

BTW, I love the Trinket and Trinket Pro. The Trinket is handy for lightweight functions.

1 Like

You mean that's what you want? The code you posted doesn't do anything like that.

a7

Of course: here you have it:


The red and blue cable are supposed to be later energy supply

Yeah- thats exactly why I chose it. <3

Thank you for your reply.
This is what actually happens
ns. :slight_smile: So the code apparently does this if I wired it correctly :smiley:

The description of what I want is in the initial post:

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)

So I think I fucked up my first program.

The code you posted does nothing like any description you have written, no matter how those descriptions are interpreted.

The code you posted will have the LED on when you are not pressing the button, and off when you are pressing the button.

Or vice versa.

Also, each LED should have its own series current limiting resistor. And 10 ohms is way too low for the roll either resistor plays in your circuit.

So maybe the thing does do what you claim. If so it is only how a bad circuit makes an Arduino board unhappy.

a7

So did i not calculate the resistors correctly? What should they actually be?

For now use 1K resistors. Three. One for each LED and one for the pushbutton.

Better: lose the resistor on the pushbutton and wire the pushbutton between the input pin and ground.

Then use

  pinMode(buttonPin, INPUT_PULLUP);

and expect to read LOW when the button is pressed and HIGH when it is not.

HTH

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.