init: So I don't lose it again

master
toerd@480 4 years ago
commit 224a10fd88

5
.gitignore vendored

@ -0,0 +1,5 @@
.pio
.ccls-cache
lib
include
test

@ -0,0 +1,15 @@
;PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = uno
framework = arduino

@ -0,0 +1,268 @@
/*
LEDs: 3 - 12
Punkte: Register
(Buttons)
Player 1: A0, 1-4 in Reg
Player 2: A1, 5-7,15 in Reg
*/
#include "Arduino.h"
#define LMAX 13
#define LMIN 3
#define LMID 8
typedef enum direction { up = 1, down = -1 } dir;
typedef struct p {
int id;
int binarynum;
int punkte;
int pressed;
} player;
player p1, p2;
int speed = 10;
// Pin connected to ST_CP of 74HC595
int latchPin = 18;
// Pin connected to SH_CP of 74HC595
int clockPin = 17;
////Pin connected to DS of 74HC595
int dataPin = 16;
void gameLoop();
int pressLoop(player *p);
void ledActive(int l, dir d);
void ledOn(int l);
void ledOff(int l);
void anticheat(int l);
void release(int l, dir d);
void setPoints(int p);
void gameEnd(player *p);
void endAnimation();
void startupAnimation();
void resetPlayer();
void setup() {
// Serial.begin(9600);
// init pins
for (int i = 3; i <= LMAX; ++i) {
pinMode(i, OUTPUT);
}
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
resetPlayer();
randomSeed(analogRead(6));
startupAnimation();
}
// int a = 0;
// void test() {
// if (digitalRead(p1.id)) {
// ledOn(8);
// }
// delay(25);
// ledOff(8);
// if (digitalRead(p2.id) && a == 0) {
// a = 1;
// digitalWrite(latchPin, LOW);
// shiftOut(dataPin, clockPin, MSBFIRST, 16);
// digitalWrite(latchPin, HIGH);
// delay(300);
// }
// if (digitalRead(p2.id) && a == 1) {
// a = 0;
// digitalWrite(latchPin, LOW);
// shiftOut(dataPin, clockPin, MSBFIRST, 0);
// digitalWrite(latchPin, HIGH);
// delay(200);
// }
// }
void loop() {
setPoints(0);
gameLoop();
}
// Setzt Status des Playerbuttons
void anticheat(int l) {
if (digitalRead(p1.id) && l != LMAX) {
p1.pressed = 1;
}
if (digitalRead(p2.id) && l != LMIN) {
p2.pressed = 1;
}
}
void release(int l, dir d) {
if (l == LMAX - 1 && d == down)
p1.pressed = 0;
if (l == LMIN + 1 && d == up)
p2.pressed = 0;
}
void ledOn(int l) { digitalWrite(l, HIGH); }
void ledOff(int l) { digitalWrite(l, LOW); }
void ledActive(int l, dir d) {
// Rand speed in der Mitte
if (l == LMID) {
speed = random(30, 120);
// speed = 800;
}
if (d == up) {
ledOff(l - 1);
} else {
ledOff(l + 1);
}
ledOn(l);
// Abfrage was zu tun ist (warten oder Knopf checken)
if (l == LMIN) {
if (pressLoop(&p2)) {
p2.binarynum >>= 1;
p2.punkte += p2.binarynum;
setPoints(p1.punkte + p2.punkte);
}
} else if (l == LMAX) {
if (pressLoop(&p1)) {
p1.binarynum >>= 1;
p1.punkte += p1.binarynum;
setPoints(p1.punkte + p2.punkte);
}
} else {
delay(speed);
anticheat(l);
release(l, d);
}
}
// ret 1 wenn in zeit gedrückt, 0 sonst
int pressLoop(player *p) {
// Falls gecheated wurde, aussetzen
if (p->pressed) {
delay(speed);
return 0;
}
// Abfrageloop
for (int i = 0; i < speed; ++i) {
if (digitalRead(p->id)) {
return 1;
}
delay(1);
}
return 0;
}
void gameLoop() {
int led = LMID;
dir d = (random(0,11) < 5) ? up : down;
// dir d = up;
while (true) {
// Richtung bei erreichen von letzter LED wechseln
if (led == LMAX && d == up) {
d = down;
} else if (led == LMIN && d == down) {
d = up;
}
// Je nach Richtung inc oder dec
led = (d == up) ? led + 1 : led - 1;
// LED aktivieren und buttons checken
ledActive(led, d);
// GAME OVER
if (p1.punkte == 15) {
gameEnd(&p1);
return;
}
if (p2.punkte == 240) {
gameEnd(&p2);
return;
}
}
}
void setPoints(int p) {
digitalWrite(latchPin, LOW);
// shift out the bits:
// 1, 2, 4, 8, 16, 32, 64, 128
shiftOut(dataPin, clockPin, MSBFIRST, p);
// take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
}
void resetPlayer() {
// Player 1 Init
p1.id = 15; // Analog 1
p1.binarynum = 16;
p1.punkte = 0;
p1.pressed = 0;
// Player 2 Init
p2.id = 14; // Analog 0
p2.binarynum = 256;
p2.punkte = 0;
p2.pressed = 0;
}
void gameEnd(player *p) {
for (int i = 0; i < 5; ++i) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
delay(80);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, p->punkte);
digitalWrite(latchPin, HIGH);
delay(80);
}
endAnimation();
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
delay(80);
resetPlayer();
}
void endAnimation() {
int speed = 25;
for (int i = 0; i < 10; ++i) {
for (int i = 0; i < 6; ++i) {
ledOn(LMID + i);
ledOn(LMID - i);
delay(speed);
ledOff(LMID + i);
ledOff(LMID - i);
}
for (int i = 0; i < 6; ++i) {
ledOn(LMIN + i);
ledOn(LMAX - i);
delay(speed);
ledOff(LMIN + i);
ledOff(LMAX - i);
}
}
}
void startupAnimation() {
for (int j = 0; j < 2; j++) {
for (int i = 1; i < 200; i *= 2) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, i);
digitalWrite(latchPin, HIGH);
delay(100);
}
}
}
Loading…
Cancel
Save