//integer variables labeling led groups and what pins they are on
int Led1 = D1;
int Led2 = D4;
int Led3 = D3;
int Led4 = D2;
int Led5 = D5;
int Led6 = D6;
int Led7 = D7;
//integer variable labeling what pin the button is on
int buttonPin = D0;
//button will randomize from 1 to 6
int buttonState;
long ran;
int timeDelay = 2000;
//open setup
void setup ()
{
//start the serial monitor
Serial.begin(115200);
//set up led pins as output
pinMode (Led1, OUTPUT);
pinMode (Led2, OUTPUT);
pinMode (Led3, OUTPUT);
pinMode (Led4, OUTPUT);
pinMode (Led5, OUTPUT);
pinMode (Led6, OUTPUT);
pinMode (Led7, OUTPUT);
//set up button pin as input
pinMode (buttonPin, INPUT_PULLUP);
// random number generator
randomSeed(esp_random());
// Print dice start in serial monitor
Serial.println("");
Serial.println("Dice Start");
}
//close setup
//open loop
void loop()
{
//read the button
int buttonState = digitalRead(buttonPin);
//checks to see what the last and current state of the button is
if (buttonState == LOW)
{
//start with all of the LED's in the off position
digitalWrite (Led1, LOW);
digitalWrite (Led2, LOW);
digitalWrite (Led3, LOW);
digitalWrite (Led4, LOW);
digitalWrite (Led5, LOW);
digitalWrite (Led6, LOW);
digitalWrite (Led7, LOW);
//when button is pressed, pick a random number from 1 to 6
ran = random(1, 7);
//when number 1 is chosen, turn on the group 4 leds and wait
if (ran == 1)
{
Serial.println("1");
digitalWrite (Led1, HIGH);
delay (timeDelay);
}
//when number 2 is chosen, turn on the group 1 leds and wait
if (ran == 2)
{
Serial.println("2");
digitalWrite (Led2, HIGH);
digitalWrite (Led7, HIGH);
delay (timeDelay);
}
//when number 3 is chosen, turn on the groups 3 and 4 leds and wait
if (ran == 3)
{
Serial.println("3");
digitalWrite (Led1, HIGH);
digitalWrite (Led2, HIGH);
digitalWrite (Led7, HIGH);
delay (timeDelay);
}
//when number 4 is chosen, turn on the groups 1 and 3 leds and wait
if (ran == 4)
{
Serial.println("4");
digitalWrite (Led2, HIGH);
digitalWrite (Led4, HIGH);
digitalWrite (Led5, HIGH);
digitalWrite (Led7, HIGH);
delay (timeDelay);
}
//when number 5 is chosen, turn on the groups 1, 3, and 4 leds and wait
if (ran == 5)
{
Serial.println("5");
digitalWrite (Led1, HIGH);
digitalWrite (Led2, HIGH);
digitalWrite (Led4, HIGH);
digitalWrite (Led5, HIGH);
digitalWrite (Led7, HIGH);
delay (timeDelay);
}
//when number 6 is chosen, turn on the groups 1, 2, and 3 leds and wait
if (ran == 6)
{
Serial.println("6");
digitalWrite (Led2, HIGH);
digitalWrite (Led3, HIGH);
digitalWrite (Led4, HIGH);
digitalWrite (Led5, HIGH);
digitalWrite (Led6, HIGH);
digitalWrite (Led7, HIGH);
}
}
}
Loading
xiao-esp32-c6
xiao-esp32-c6