Hey leute,
ich will ein kleine Spiel bauen bei dem es um Reaktiongeschwindigkeit geht aber ich hab nun einen punkt erreicht an dem ich total stucked bin und nicht mehr weiter weiss
Um was geht es ?:
also ich habe insgesamt 8 LED's ( 4 Rote = R; 4 Grüne = G )
und einen Joystick.
( Jede LED hat ihren eignen Vorwiderstand )
Das Spielfeld von oben:
G
R
G R R G
R
G
Was ist das Ziel ?:
Wenn eine grüne LED leuchtet muss man schnell mit dem Joystick die dazugehörige rote zum leuchten bringen, also bevor die grüne halt wieder aus geht.
Und so geht das ganz immer weiter bin man es einmal nicht schaffen sollte rechtzeitig zu navigieren.
Was funktioniert bisher ?:
Ich kann mittlerweile alle roten LED's über den curser ansteuern.
Was funktioniert nicht ?:
Dieses Problem befasst sich ausschließlich mit dem äußeren ring, also den Grünen LED's!
Ich kann leider die grünen LED's nicht für eine bestimmte zeit in einer willkürlichen Reihenfolge aufleuchten lassen.
Da die delay() Funktion den Prozessor monopolisieren würde (und somit alles stillsteht bis das delay abgewartet ist) muss ich eine State Machine verwenden.
Hier mein Code: (dessen Ziel es ist die Grünen LED's random ein und aus zu schalten, es soll immer nur eine led leuchten.)
int UP_O = 8;
int RIGHT_O = 9;
int DOWN_O = 10;
int LEFT_O = 11;
int UP_O_STATE = LOW;
int RIGHT_O_STATE = LOW;
int DOWN_O_STATE = LOW;
int LEFT_O_STATE = LOW;
unsigned long previousMillis_UP_O = 0;
unsigned long previousMillis_RIGHT_O = 0;
unsigned long previousMillis_DOWN_O = 0;
unsigned long previousMillis_LEFT_O = 0;
long OnTime = 300;
long OffTime = 300;
int counter = 100;
int count = 100;
int random_position = 1;
void setup() {
pinMode(UP_O, OUTPUT);
pinMode(RIGHT_O, OUTPUT);
pinMode(DOWN_O, OUTPUT);
pinMode(LEFT_O, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(count == 0)
{
count = 1000;
random_position = random(4);
return random_position;
}
else
{
count = count -1;
}
if (random_position == 0)
{
int counter = 1000;
while (counter > 0)
{
OUTSIDE_UP();
counter = counter -1;
}
}
else if (random_position == 1)
{
int counter = 1000;
while (counter > 0)
{
OUTSIDE_RIGHT();
counter = counter -1;
}
}
else if (random_position == 2)
{
int counter = 1000;
while (counter > 0)
{
OUTSIDE_DOWN();
counter = counter -1;
}
}
else if (random_position == 3)
{
int counter = 1000;
while (counter > 0)
{
OUTSIDE_LEFT();
counter = counter -1;
}
}
}
void OUTSIDE_UP(){
unsigned long currentMillis = millis();
if((UP_O_STATE == HIGH) && (currentMillis - previousMillis_UP_O >= OnTime))
{
UP_O_STATE = LOW; // Turn it off
return UP_O_STATE;
previousMillis_UP_O = currentMillis; // Remember the time
digitalWrite(UP_O, UP_O_STATE); // Update the actual LED
}
else if ((UP_O_STATE == LOW) && (currentMillis - previousMillis_UP_O >= OffTime))
{
UP_O_STATE = HIGH; // turn it on
return UP_O_STATE;
previousMillis_UP_O = currentMillis; // Remember the time
digitalWrite(UP_O, UP_O_STATE); // Update the actual LED
}
}
void OUTSIDE_RIGHT(){
unsigned long currentMillis = millis();
if((RIGHT_O_STATE == HIGH) && (currentMillis - previousMillis_RIGHT_O >= OnTime))
{
RIGHT_O_STATE = LOW; // Turn it off
previousMillis_RIGHT_O = currentMillis; // Remember the time
digitalWrite(RIGHT_O, RIGHT_O_STATE); // Update the actual LED
}
else if ((RIGHT_O_STATE == LOW) && (currentMillis - previousMillis_RIGHT_O >= OffTime))
{
RIGHT_O_STATE = HIGH; // turn it on
previousMillis_RIGHT_O = currentMillis; // Remember the time
digitalWrite(RIGHT_O, RIGHT_O_STATE); // Update the actual LED
}
}
void OUTSIDE_DOWN(){
unsigned long currentMillis = millis();
if((DOWN_O_STATE == HIGH) && (currentMillis - previousMillis_DOWN_O >= OnTime))
{
DOWN_O_STATE = LOW; // Turn it off
previousMillis_DOWN_O = currentMillis; // Remember the time
digitalWrite(DOWN_O, DOWN_O_STATE); // Update the actual LED
}
else if ((DOWN_O_STATE == LOW) && (currentMillis - previousMillis_DOWN_O >= OffTime))
{
DOWN_O_STATE = HIGH; // turn it on
previousMillis_DOWN_O = currentMillis; // Remember the time
digitalWrite(DOWN_O, DOWN_O_STATE); // Update the actual LED
}
}
void OUTSIDE_LEFT(){
unsigned long currentMillis = millis();
if((LEFT_O_STATE == HIGH) && (currentMillis - previousMillis_LEFT_O >= OnTime))
{
LEFT_O_STATE = LOW; // Turn it off
previousMillis_LEFT_O = currentMillis; // Remember the time
digitalWrite(LEFT_O, LEFT_O_STATE); // Update the actual LED
}
else if ((LEFT_O_STATE == LOW) && (currentMillis - previousMillis_LEFT_O >= OffTime))
{
LEFT_O_STATE = HIGH; // turn it on
previousMillis_LEFT_O = currentMillis; // Remember the time
digitalWrite(LEFT_O, LEFT_O_STATE); // Update the actual LED
}
}