25. September 2012
Mit den Knöpfen links und rechts lässt sich der Regler bedienen (einmaliges drücken oder daraufbleiben). Durch gleichzeitige Klicken der beiden Knöpfe wird in den Game Modus gewechselt. main.ino#include
#define BUTTON_UP_PIN 3
#define BUTTON_DOWN_PIN 2
#define LATCH_PIN 8 //Pin zu ST_CP vom 74HC595
#define CLOCK_PIN 10 //12 //Pin zu SH_CP vom 74HC595
#define DATA_PIN 9 //11 //Pin zu DS vom 74HC595
#define PIN_9 12
#define PIN_10 13
/*** Global Variables ***/
int level = 0;
unsigned long currentMillis;
unsigned long previousMillis;
int remoteMode = true; //true is remote mode, false is game mode
int buttonDownState = LOW;
int buttonUpState = LOW;
long buttonDownMillis = 0;
long buttonUpMillis = 0;
/*** Game Variables ***/
int counter = 0;
bool forward = true;
int scope = 0;
int wins = 0;
/* Config */
long speedMillis = 125;
long speedStepMillis = 25;
long buttonStateMillisGame = 50;
/*** Config ***/
long buttonStateMillis = 150;
/*** Config end ***/
void setup() {
pinMode(BUTTON_UP_PIN, INPUT);
pinMode(BUTTON_DOWN_PIN, INPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(PIN_9, OUTPUT);
pinMode(PIN_10, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(remoteMode == true) {
readInput();
drawLevels();
} else {
currentMillis = millis();
if(currentMillis - previousMillis > (speedMillis - (speedStepMillis * wins))) {
previousMillis = currentMillis;
registerWrite(counter, HIGH); // Entsprechende LED anmachen
count();
}
checkButtonStates();
}
}
void readInput() {
buttonDownState = digitalRead(BUTTON_DOWN_PIN);
buttonUpState = digitalRead(BUTTON_UP_PIN);
currentMillis = millis();
if(buttonDownState == HIGH && buttonUpState == HIGH) {
switchMode();
} else if(buttonDownState == HIGH) {
if(buttonDownMillis == 0) buttonDownMillis = currentMillis;
if(level > 0) {
if(currentMillis - buttonDownMillis > buttonStateMillis) {
level--;
buttonDownMillis = 0;
}
}
} else if(buttonUpState == HIGH) {
if(buttonUpMillis == 0) buttonUpMillis = currentMillis;
if(level < 9) { if(currentMillis - buttonUpMillis > buttonStateMillis) {
level++;
buttonUpMillis = 0;
}
}
}
}
void drawLevels() {
byte bitsToSend = 0;
digitalWrite(LATCH_PIN, LOW);
for(int i = 0; i
if(i < 8) bitWrite(bitsToSend, i, HIGH);
else if(i == 8) digitalWrite(PIN_9, HIGH);
else if(i == 9) digitalWrite(PIN_10, HIGH);
}
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend);
digitalWrite(LATCH_PIN, HIGH);
if(level < 9) digitalWrite(PIN_10, LOW);
if(level < 8) digitalWrite(PIN_9, LOW);
}
void switchMode() {
int bitsToSend;
int state;
if(remoteMode == true) remoteMode = false;
else remoteMode = true;
/*** Blink when changing mode ***/
for(int i = 0; i
if(i % 2 == 0) {
bitsToSend = 255;
state = HIGH;
} else {
bitsToSend = 0;
state = LOW;
}
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend);
digitalWrite(PIN_9, state);
digitalWrite(PIN_10, state);
digitalWrite(LATCH_PIN, HIGH);
delay(buttonStateMillis);
}
}
game.ino
void registerWrite(int _whichPin, int _whichState)
{
if(_whichPin > 7) {
if(_whichPin == 8) {
digitalWrite(PIN_9, HIGH);
digitalWrite(PIN_10, LOW);
} else {
digitalWrite(PIN_10, HIGH);
digitalWrite(PIN_9, LOW);
}
_whichPin = 1;
_whichState = 0;
} else {
digitalWrite(PIN_10, LOW);
digitalWrite(PIN_9, LOW);
}
byte bitsToSend = 0; // Dieses Byte hat acht Bits also: 00000000
digitalWrite(LATCH_PIN, LOW);
bitWrite(bitsToSend, _whichPin, _whichState); // Hier wird das entsprechende Bit im Byte gesetzt (z.B. 00100000)
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend);
digitalWrite(LATCH_PIN, HIGH);
}
void win(int _scope) {
byte bitsToSend;
if((scope == 2 && _scope == 1) || (scope == 1 && _scope == 2)) {
bitsToSend = B11111111;
digitalWrite(PIN_9, HIGH);
digitalWrite(PIN_10, HIGH);
scope = 0;
wins++;
} else if (_scope == 1) {
bitsToSend = B00011111;
scope = 1;
} else if (_scope == 2) {
bitsToSend = B11100000;
digitalWrite(PIN_9, HIGH);
digitalWrite(PIN_10, HIGH);
scope = 2;
}
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend);
digitalWrite(LATCH_PIN, HIGH);
delay(speedMillis * 2);
if(wins == 6) {
delay(20000);
wins = 0;
}
registerWrite(1, 0);
digitalWrite(PIN_9, LOW);
digitalWrite(PIN_10, LOW);
}
void checkButtonStates() {
buttonDownState = digitalRead(BUTTON_DOWN_PIN);
buttonUpState = digitalRead(BUTTON_UP_PIN);
currentMillis = millis();
if(buttonDownState == HIGH && buttonUpState == HIGH) {
switchMode();
}
if(buttonDownState == HIGH && buttonUpState == LOW) {
if(buttonDownMillis == 0) buttonDownMillis = currentMillis;
if(counter == 0) {
if(currentMillis - buttonDownMillis < buttonStateMillisGame - (5 * wins)) { win(1); } } else { if(wins > 0) wins--;
}
} else {
buttonDownMillis = 0;
}
if(buttonUpState == HIGH && buttonDownState == LOW) {
if(buttonUpMillis == 0) buttonUpMillis = currentMillis;
if(counter == 9) {
if(currentMillis - buttonUpMillis < buttonStateMillisGame - (5 * wins)) { win(2); } } else { if(wins > 0) wins--;
}
} else {
buttonUpMillis = 0;
}
}
void count() {
if(counter >= 9) forward = false;
else if(counter
if(forward == true) counter++;
else counter--;
}