//======CODE 1=========
//GLOBAL VARIABLES
const byte BUTTON8 = 8; // our button pin
const byte BUTTON9 = 9; // our button pin
const byte LED = 13; // LED (built-in on Uno)
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 1000; // wait to turn on LED
unsigned long turnOffDelay = 1000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
//=====================
//======CODE 2=========
// defines pins numbers
const int dirPin = 2;
const int stepPin = 3;
const int enPin = 5;
const int switchOne = 8;
const int switchTwo = 9;
int lastSwitchPressed = -1;
int p1buttonState = 0; // current state of the button
int lastp1buttonState = 0; // previous state of the button
int p2buttonState = 0; // current state of the button
int lastp2buttonState = 0; // previous state of the button
bool bPress = false;
bool isForward = false;
bool isBackward = false;
//======================
void setup() {
setup1();
setup2();
}
void loop() {
loop1();
loop2();
}
void setup2(){
pinMode(switchOne, INPUT_PULLUP);
pinMode(switchTwo, INPUT_PULLUP);
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
}
void loop2(){
isForward = false;
isBackward = false;
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
if (lastSwitchPressed != switchOne && p1ButtonPress()) {
lastSwitchPressed = switchOne;
digitalWrite(dirPin, HIGH);
delay(5);
}
if (lastSwitchPressed != switchTwo && p2ButtonPress()) {
lastSwitchPressed = switchTwo;
digitalWrite(dirPin, LOW);
delay(5);
}
if (isForward || isBackward) {
for (int x = 0; x < 100; x++) { // # of turns(200=1turn)
digitalWrite(stepPin, HIGH);
delayMicroseconds(2500); // speed
digitalWrite(stepPin, LOW);
delayMicroseconds(2500); // speed
loop1();
}
}
}
bool p1ButtonPress() {
bool isPress = false;
// compare the p1buttonState to its previous state
if (p1buttonState != lastp1buttonState) {
// if the state has changed, increment the counter
if (p1buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
isPress = true;
isForward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp1buttonState = p1buttonState;
return isPress;
}
bool p2ButtonPress() {
bool isPress = false;
// compare the p1buttonState to its previous state
if (p2buttonState != lastp2buttonState) {
// if the state has changed, increment the counter
if (p2buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
isPress = true;
Serial.println("Plaer Two score");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
isBackward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp2buttonState = p2buttonState;
return isPress;
}
void setup1(){
pinMode(BUTTON8, INPUT_PULLUP);
pinMode(BUTTON9, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop1(){
// get the time at the start of this loop()
unsigned long currentMillis = millis();
// check the button
if (digitalRead(BUTTON8) == LOW || digitalRead(BUTTON9) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LED, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(LED, LOW);
}
}
}