[GELÖST] Problem erstes Arduino Projekt

Hallo zusammen,
ich muss für die Uni ein kleines Arduino Spiel basteln. Es sollen 3 LED zufällig aufblinken und sobald ein Taster gedrückt wird wenn alle 3 LED aufleuchten, aufleuchtzeit von 400 ms um 20ms verringert. Wenn nicht alle 3 LED blinken, wird der Timer wieder auf 400ms gesetzt.
Soweit so klar.
Ich habe die Software schon geschrieben und auf dem Arduino an der Uni funktionierte sie auch. Zu Hause bei meinem Arduino funktioniert das ganze nicht mehr obwohl die Schaltung komplett gleich ist.
Mein Problem ist, dass der Taster nicht oder nur 1x erkannt wird.
Kann es sein dass folgender Taster http://www.conrad.de/ce/de/product/707732/Miniatur-Print-Taster-24-VDC-001-A-L-x-B-x-H-185-x-10-x-7-mm-Schwarz-gerade?ref=searchDetail nicht mit dem Arduino Nano V3 kompatibel ist?
Ich bin noch recht neu in der Materie (erstes Semester Computer und Kommunikationstechnik).

Im Anhang ein Bild der Schaltung welches ich mit der Fritzing Software erstelt habe.

Für Hilfe wäre ich sehr dankbar :slight_smile:

Auf dem Fritzing Breadboard ist der Taster falsch eingezeichnet.

Bei Conrad hast du dir scheinbar einen Taster bestellt, der nur 2polig ist, somit nur 1 Schließer.

Schließe wie folgt an.

   5V ---------------------O Anschluss Button 2
              ___
   GND ------|___|---------O Anschluss Button 1
                   |
                   |
   DI -------------'

Hast Du den internen Pullup Widerstand aktiviert?

Grüße Uwe

Als erstes schonmal danke für Eure Antworten. Ich habe den Taster jetzt mal nur mit einer LED getestet, und da tut er was er soll. Drücken leuchtet. Loslassen aus. Den internen Pullup habe ich nicht aktiviert, ich habe einen 10k? Widerstand vor den Taster gesteckt.

Hab euch mal 2 Videos und den Code angehängt:

https://vimeo.com/78802413 Taster nur an einer LED über +5V und gnd angeschlossen.

https://vimeo.com/78802457 Taster im laufenden Spiel.

Edit: was ich jetzt gerade gemerkt habe ist, dass er mir auch eine println('') senden wenn ich am Widerstand wackele obwohl der Taster offen ist. Wie kann das sein? Seufz.

Edit 2: https://vimeo.com/78803527 Der Taster scheint zu flackern.

const int buttonPin = 2;
const int ledPin =  5; 
int buttonState = 0; 

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);  
    Serial.println("BUTTON DOWN");
  } 
  else {
    Serial.println("BUTTON UP");
    digitalWrite(ledPin, LOW); 
  }
}

// constant variables
const int buttonPin = 2;      // the number of the pushbutton pin
const int ledPin = 13;        // the number of the LED pin
const int led1 = 4;
const int led2 = 5;
const int led3 = 6;

// variables will change
char outputString[50];        // string to store the messages 
int level = 1;                // the level number, how often the player pushed to the right time
int buttonState;              // the current reading from the input pin
int lastButtonState = LOW;    // the previous reading from the input pin
int time = 400;               // start on-time of the blinking leds

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long previousMillis = 0;      // varable to store the milliseconds from the last loop-round
long lastDebounceTime = 0;    // the last time the output pin was toggled
long debounceDelay = 50;      // the debounce time; increase if the output flickers

void setup() 
{
  // serial communication initialize with 9600 Baud Rate
  Serial.begin(9600);

  // initialize the pin-states
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  // read analog value from floating pin for random-seed
  randomSeed(analogRead(0));
}

void loop() 
{
  // read the current time since the start of the program in milliseconds (with overload of the variable)
  unsigned long currentMillis = millis();
 
  // calculate the 400ms(only at start!) for the on-time of the blinking leds
  if ( (currentMillis - previousMillis) > time )  
  {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // turn the leds off and a little delay for optical detection that the random led sequence has changed
    ledsOff();
    delay(35);
    
    // set the random established values, either 1 or 2, because the digitalWrite uses binary values, the 1 counts as on and the 2 counts as off for the led
    digitalWrite(led1, random(0,3));
    digitalWrite(led2, random(0,3));
    digitalWrite(led3, random(0,3));
  }

  // read the state of the switch into a local variable
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if ( reading != lastButtonState )
    lastDebounceTime = millis(); // reset the debouncing timer
  
  // delay of 50ms for isolating noise
  if ( (millis() - lastDebounceTime) > debounceDelay ) 
  {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state

    // if the button state has changed
    if (reading != buttonState) 
    {
      buttonState = reading;

      // only do the sequence if the new button state is HIGH (depends on hardware)
      if ( buttonState == HIGH ) 
      {
        // check if all leds are on (HIGH), so that the button was pushed to the right time
        if ( (digitalRead(led1)== HIGH) && (digitalRead(led2) == HIGH) && (digitalRead(led3) == HIGH) )
        { 
          // decrease the on-time of the leds by 20ms(0.02s)
          time = time - 20;
          
          // light up the led 13 on the arduino itself, for optical detection that the button is pushed to the right time
          digitalWrite(ledPin, HIGH);
          
          // send string over the serial line to the pc for more detailied information, what level and time the user accomplished
          sprintf(outputString, "Level: %d \r\nZeit: %d ms\r\n", level++,time);
          Serial.println(outputString);
          //Serial.println("Level:");
          //Serial.println(level++);
          //Serial.println("Zeit:");
          //Serial.println(time);
      
        } else
        {          
          // reset the led lightnings
          ledsOff();
          
          // send a serial message for better recognition of a failed push
          Serial.println("FAIL! Restart");
          
          // for optical detection of a failed push and reset of the game
          delay(2000); 
          
          level = 1;
          time = 400;
        }
      }
    }
  }
  
  // save the last button state
  lastButtonState = reading;
}

// function for deactivation of all three leds at the same time, for better code
void ledsOff()
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(ledPin,LOW);
}

Knoffel:
Als erstes schonmal danke für Eure Antworten. Ich habe den Taster jetzt mal nur mit einer LED getestet, und da tut er was er soll. Drücken leuchtet. Loslassen aus. Den internen Pullup habe ich nicht aktiviert, ich habe einen 10k? Widerstand vor den Taster gesteckt.

Vor den Taster stecken nutzt nichts. Der Widerstand muß den Eingang bei nicht gedrückten Taster auf +5V (oder auf Masse) ziehen. Dazu muß der Widerstand vom Eingangspin zu +5V (oder Masse) gesteckt sein. Der Taster muß dann zwischen Eingang und Masse (oder +5V) gesteckt sein. Du liest dann bei betätigten Taster LOW (oder HIGH). Nur so ist der Eingang sicher auf einem definierten Potenzial = Spannung angeschlossen. So wie Du ihn angeschlossen hast, ist bei nicht betätigten Taster der Eingang in der Luft und mißt Stöhrungen die zufällig mal HIGH sind und andere male wieder nicht.

Grüße Uwe

Ich danke euch beiden, jetzt hat es bei mir klick gemacht.
Ging von einer Analogen Schaltung aus: Schalter offen= kein strom, schalter geschlossen= strom.
Aber das mit dem Widerstand an gnd macht durchaus Sinn :wink:

Vielen lieben dank.

Tja, der Unterschied zwischen Strom und Spannung.

Wenn du ein Stück Draht "als Antenne" in einen Arduino Pin steckst, sieht der eine Spannung ungleich 0, die gerne so hoch sein kann, dass er sie als HIGH interpretiert.
Auch wenn da natürlich kein Strom fliesst, der eine LED zum Leuchten brächte.

Wenn dich dann das "invertierte" Verhalten beim Aktivieren der internen Pullup-Widerstände nicht mehr verwirrt,
bist du einen wesentlichen Schritt weiter.

Dass ein Taster mehrmals schaltet, wenn du ihn einmal drückst, hast du ja auch schon gemerkt.
-- Das Stichwort heisst prellen ( bounce )

Kann man einfach oder kompliziert lösen ...

Viel Spaß weiterhin :slight_smile: