mercredi 20 juin 2018

Store a random value in a for loop in arduino ide

My project is a LED "wheel of fortune", when the button is not pressed the LEDs light up randomly but when I press the button the LEDs are supposed to light up in a circle and then stop at a random point. I'm using a altered example code for arrays from the arduino sites and almost got it working.

Whats missing is: hold the last lit up LED (the one where it stops randomly after a few times going round in circles). I know I should store the last used LED and then tell it to delay for a bit. I got it working in the else-loop but don't know how to put that same code in the for-loop.

Code follows here. I hope I made the question clear, if not please ask!

/*
Arrays

Demonstrates the use of an array to hold pin numbers in order to iterate over
the pins in a sequence. Lights multiple LEDs in sequence, then in reverse.

Unlike the For Loop tutorial, where the pins have to be contiguous, here the
pins can be in any random order.

The circuit:
- LEDs from pins 2 through 7 to ground

created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Array

*/
int taster = 8;
int tasterstatus = 0;
int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = {
    // 2, 7, 4, 6, 5, 3
    2,3,4,5,6
};       // an array of pin numbers to which LEDs are attached
int pinCount = 5;           // the number of pins (i.e. the length of the array)



void setup() {
    // the array elements are numbered from 0 to (pinCount - 1).
    // use a for loop to initialize each pin as an output:
    pinMode(taster, INPUT);
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
        pinMode(ledPins[thisPin], OUTPUT);
    }
}

void loop()
{
    tasterstatus = digitalRead(taster);
    if (tasterstatus == HIGH)
    {
        for (int kreisrund = 0; kreisrund < 5; kreisrund++)
        {
            // loop from the lowest pin to the highest:
            for (int thisPin = 0; thisPin < pinCount; thisPin++)
            {
                // turn the pin on:
                digitalWrite(ledPins[thisPin], HIGH);
                delay(timer / 2);
                // turn the pin off:
                digitalWrite(ledPins[thisPin], LOW);
            }
        }

        for (int kreisrund = 0; kreisrund < 1; kreisrund++)
        {
            // loop from the lowest pin to the highest:
            for (int thisPin = 0; thisPin < pinCount; thisPin++)
            {
                // turn the pin on:
                digitalWrite(ledPins[thisPin], HIGH);
                delay(timer * 3);
                // turn the pin off:
                digitalWrite(ledPins[thisPin], LOW);
            }

            // WHERE TO PUT THE INDEX?! 
            for (int thisPin = 0; thisPin < random(0, 5); thisPin++)
            {
                // turn the pin on:
                digitalWrite(ledPins[thisPin], HIGH);
                delay(timer * 5);
                // turn the pin off:
                digitalWrite(ledPins[thisPin], LOW);
            }
        }
    }
    else
    {
        int index;
        index = random(5);

        digitalWrite(ledPins[index], HIGH);
        delay(timer);
        digitalWrite(ledPins[index], LOW);

        /*// loop from the lowest pin to the highest:
        //for (int thisPin = 0; thisPin < pinCount; thisPin++) {
        // turn the pin on:
        digitalWrite(ledPins[random(0,5)], HIGH);
        delay(timer/2);
        // turn the pin off:
        digitalWrite(ledPins[random(0,5)], LOW);
        */
    }
}




Aucun commentaire:

Enregistrer un commentaire