Arduino – Un semplice gioco (sketch)

Supportami su Patreon!
Seguimi anche sulla pagina di facebook! e sul canale di youtube

Di seguito potete trovare lo sketch (il programma) che va caricato nel microcontrollore affinchè il tutto funzioni.


#include
Servo myServo;
int const switchUnoPin = 2;
int const switchDuePin = 3;
int const switchTrePin = 4;
int const LEDUnoPin = 6;
int const LEDDuePin = 7;
int const LEDTrePin = 8;
int const LEDWinPin = 13;
int const LEDLoosePin = 12;
int switchUnoState = 0;
int switchDueState = 0;
int switchTreState = 0;
int angle = 0;
int randomNumber = 0;

void setup() {
myServo.attach(10);
Serial.begin(9600);
pinMode(switchUnoPin, INPUT);
pinMode(switchDuePin, INPUT);
pinMode(switchTrePin, INPUT);
pinMode(LEDUnoPin, OUTPUT);
pinMode(LEDDuePin, OUTPUT);
pinMode(LEDTrePin, OUTPUT);
pinMode(LEDWinPin, OUTPUT);
pinMode(LEDLoosePin, OUTPUT);
randomSeed(analogRead(0));
randomNumber = random(3);

}
void loop() {
angle = 0;
myServo.write(angle);
digitalWrite(LEDWinPin, LOW);
digitalWrite(LEDLoosePin, LOW);
if(randomNumber == 0) {
digitalWrite(LEDUnoPin, HIGH);
digitalWrite(LEDDuePin, LOW);
digitalWrite(LEDTrePin, LOW);
} else if(randomNumber == 1) {
digitalWrite(LEDUnoPin, LOW);
digitalWrite(LEDDuePin, HIGH);
digitalWrite(LEDTrePin, LOW);
} else if (randomNumber == 2) {
digitalWrite(LEDUnoPin, LOW);
digitalWrite(LEDDuePin, LOW);
digitalWrite(LEDTrePin, HIGH);
}

switchUnoState = digitalRead(switchUnoPin);
switchDueState = digitalRead(switchDuePin);
switchTreState = digitalRead(switchTrePin);

if(switchUnoState == 1 && switchDueState == 0 && switchTreState == 0) {
angle = 45;
myServo.write(angle);
if(randomNumber == 0) {
digitalWrite(LEDWinPin, HIGH);
delay(1000);
randomNumber = random(3);
}else {
digitalWrite(LEDLoosePin, HIGH);
delay(1000);
randomNumber = random(3);
}

} else if(switchDueState == 1 && switchUnoState == 0 && switchTreState == 0) {
angle = 90;
myServo.write(angle);
if(randomNumber == 1) {
digitalWrite(LEDWinPin, HIGH);
delay(1000);
randomNumber = random(3);
}else {
digitalWrite(LEDLoosePin, HIGH);
delay(1000);
randomNumber = random(3);
}
} else if(switchTreState == 1 && switchUnoState == 0 &&switchDueState == 0) {
angle = 135;
myServo.write(angle);
if(randomNumber == 2) {
digitalWrite(LEDWinPin, HIGH);
delay(1000);
randomNumber = random(3);
}else {
digitalWrite(LEDLoosePin, HIGH);
delay(1000);
randomNumber = random(3);
}
} else if(switchUnoState == 0 && switchDueState == 0 && switchTreState == 0) {
angle = 0;
myServo.write(angle);
} else {
angle = 180;
myServo.write(angle);
digitalWrite(LEDLoosePin, HIGH);
delay(1000);
randomNumber = random(3);

}

delay(15);

One thought on “Arduino – Un semplice gioco (sketch)

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *