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