teensy 2++....Why dont my leds work ???

toreil

Member
Hi:
Just trying a script with buttons and led's
Should think its easy, but i cant get the leds to work.
If i use the old blink script all is good
The buttons work i get conf in the monitor.
Led 1 works but not 2,3 and 4..
think i tested everything ,,,
here is the code
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 15; //From Left
const int buttonPin2 = 16;
const int buttonPin3 = 17;
const int buttonPin4 = 45;
const int buttonPin5 = 39;
const int ledPin1 = 26;
const int ledPin2 = 25;
const int ledPin3 = 24;
const int ledPin4 = 23;

// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;


void setup() {
// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
Serial.begin(9600);
// LED TEST
// read the state of the pushbutton value:



}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == LOW) {
delay(200);
digitalWrite(ledPin1, HIGH);
Serial.println(F("button 1")); }


if (buttonState2 == LOW) {
delay(200);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
Serial.println(F("button 2")); }

if (buttonState3 == LOW) {
delay(200);
Serial.println(F("button 3"));
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
}

if (buttonState4 == LOW) {
delay(200);
Serial.println(F("button 4"));
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
}

if (buttonState5 == LOW) {
delay(200);
Serial.println(F("button 5"));
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin1, HIGH);
}
}// LOOP

tore
 
Try this code. It's a modified version of the Blink example. Once a second it turns the built-in led and the four leds on pins 23, 24, 25, and 26 on and off. If they don't all flash on and off together, you have a wiring problem or dead output pins.

Code:
/* LED Blink, Teensyduino Tutorial #1
   http://www.pjrc.com/teensy/tutorial.html

   This example code is in the public domain.

Modified by El_Supremo
*/

const int ledPin1 = 26;
const int ledPin2 = 25;
const int ledPin3 = 24;
const int ledPin4 = 23;

uint8_t pins[] = {LED_BUILTIN, ledPin1, ledPin2, ledPin3, ledPin4};

void setup()
{
  // initialize the digital pins as outputs.
  for(int i = 0; i < sizeof(pins); i++) {
    pinMode(pins[i], OUTPUT);
  }
}

void loop()
{
  // Turn on all the leds and then wait one second
  for(int i = 0; i < sizeof(pins); i++) {
    digitalWrite(pins[i], HIGH);
  }
  delay(1000);
  // turn off all the leds and then wait one second
  for(int i = 0; i < sizeof(pins); i++) {
    digitalWrite(pins[i], LOW);
  }
  delay(1000);
}

Pete
 
The buttons work i get conf in the monitor.
Led 1 works but not 2,3 and 4..
think i tested everything ,,,

Can you tell show us what actually happens? I'm really not getting a clear picture from only words "Led 1 works but not 2,3 and 4.."

Maybe a quick cell phone video uploaded to youtube would help? You can set a youtube video to "unlisted" which lets anyone watch if they have the link, but doesn't promote it on their site to the general public. As you record the video, maybe speak about what you wanted the LEDs to do versus the way they are not working.

Or if you clearly write how you want the LEDs to work, maybe we can compare your description of the desired result to the code. The main problem is we can't know how you wanted the LEDs to actually work. We need that understanding to be able to help you with the code.
 
"Led 1 works but not 2,3 and 4.."

All work, but you don't see it. You switch them on and - without delay - off. That's too fast to see it.
You just have the delay() s on the wrong places.

if (buttonState1 == LOW) {
delay(200); // where is digitalWrite(ledPin1, LOW); ??
digitalWrite(ledPin1, HIGH);

Serial.println(F("button 1")); }


if (buttonState2 == LOW) {
delay(200);
digitalWrite(ledPin1, LOW);
//insert delay(200); here:
delay(200);
digitalWrite(ledPin2, HIGH)
;
Serial.println(F("button 2")); }

.. same for the others...

Everything works, but you can't see it. You turn it on and—without delay—turn it off again immediately. It happens too fast to see.

You simply have the delay() commands in the wrong places.
 
Back
Top