first commit
This commit is contained in:
119
hw/bolak/bolak.ino
Normal file
119
hw/bolak/bolak.ino
Normal file
@@ -0,0 +1,119 @@
|
||||
#include <Stepper.h>
|
||||
|
||||
|
||||
#include <ezButton.h>
|
||||
|
||||
|
||||
ezButton fcP1B(8);
|
||||
ezButton fcP1T(9);
|
||||
ezButton resetBtn(6);
|
||||
ezButton P1S1(7);
|
||||
ezButton P1S2(30);
|
||||
ezButton P1S3(31);
|
||||
|
||||
|
||||
// Configuración del motor paso a paso
|
||||
const int pasosPorRevolucion = 200; // Depende de tu motor
|
||||
Stepper motor(pasosPorRevolucion, 4, 5); // Pines del Arduino al driver del motor
|
||||
|
||||
//Pins Gameover
|
||||
int pinGameOverLight = 11;
|
||||
int pinGameOverSiren = 10;
|
||||
|
||||
// Variables de estado
|
||||
bool estadoPulsadorAvance = false;
|
||||
bool estadoPulsadorReset = false;
|
||||
bool estadoAnteriorAvance = false;
|
||||
|
||||
//Variables de motor
|
||||
int remainingSteps = 0;
|
||||
const int MOTOR_INCREMENT = 50;
|
||||
const int MOTOR_ONE_LAP = 6400;
|
||||
int estadoFcP1B = 0;
|
||||
int estadoFcP1T = 0;
|
||||
|
||||
// Variables de modo
|
||||
enum modes {PLAYING, RESET, GAMEOVER};
|
||||
modes currentMode = PLAYING; // playing, reset
|
||||
|
||||
void setup() {
|
||||
pinMode(pinGameOverLight, OUTPUT);
|
||||
pinMode(pinGameOverSiren, OUTPUT);
|
||||
disableGameOver();
|
||||
|
||||
motor.setSpeed(2000); // Velocidad del motor en RPM
|
||||
fcP1B.setDebounceTime(50); // set debounce time to 50 milliseconds
|
||||
fcP1T.setDebounceTime(50); // set debounce time to 50 milliseconds
|
||||
resetBtn.setDebounceTime(50); // set debounce time to 50 milliseconds
|
||||
Serial.begin(9600);
|
||||
Serial.println("hasiera");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
fcP1B.loop();
|
||||
fcP1T.loop();
|
||||
resetBtn.loop();
|
||||
P1S1.loop();
|
||||
P1S2.loop();
|
||||
P1S3.loop();
|
||||
|
||||
|
||||
estadoFcP1B = fcP1B.getState();
|
||||
estadoFcP1T = fcP1T.getState();
|
||||
switch(currentMode){
|
||||
case RESET:
|
||||
modeReset();
|
||||
break;
|
||||
case GAMEOVER:
|
||||
modeGameOver();
|
||||
break;
|
||||
default:
|
||||
modePlaying();
|
||||
break;
|
||||
}
|
||||
|
||||
handleMotor();
|
||||
|
||||
if(resetBtn.isReleased()){
|
||||
currentMode = RESET;
|
||||
}
|
||||
|
||||
}
|
||||
void handleMotor(){
|
||||
if(estadoFcP1T){
|
||||
currentMode = GAMEOVER;
|
||||
return;
|
||||
}
|
||||
if(remainingSteps > 0) {
|
||||
motor.step(MOTOR_INCREMENT);
|
||||
remainingSteps -= MOTOR_INCREMENT;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void modeReset(){
|
||||
disableGameOver();
|
||||
if(!estadoFcP1B){
|
||||
motor.step(-MOTOR_INCREMENT);
|
||||
} else {
|
||||
currentMode = PLAYING;
|
||||
}
|
||||
}
|
||||
void modePlaying(){
|
||||
if(P1S1.isReleased() || P1S2.isReleased() || P1S3.isReleased()){
|
||||
remainingSteps += MOTOR_ONE_LAP;
|
||||
}
|
||||
}
|
||||
|
||||
void modeGameOver(){
|
||||
remainingSteps = 0;
|
||||
enableGameOver();
|
||||
}
|
||||
void enableGameOver(){
|
||||
digitalWrite(pinGameOverSiren, LOW);
|
||||
digitalWrite(pinGameOverLight, LOW);
|
||||
}
|
||||
void disableGameOver(){
|
||||
digitalWrite(pinGameOverSiren, HIGH);
|
||||
digitalWrite(pinGameOverLight, HIGH);
|
||||
}
|
||||
Reference in New Issue
Block a user