#include "Wire.h"
// please write comments in english
//pin selection
int rot=11; // <- english please
int schwarz=10; // <- english please
int reset=9;
//paths
//global variables
int path[47];
int num = 0;
int step = 0;
int current_move = 0;
byte inputs = 0;
void setup() {
// init path
// first we do a random number between 0 and 47
// but II think that is not right
// theplayer needs to reach the center so we alwys need to make a step
// you need the numbering and a system how we can select numbers around that number
// e.g.
//
// 1 | 2 | 3
// —————————
// 4 | 5 | 6
// —————————
// 7 | 8 | 9
//
// if we start at 1 we only can select 2,4
// if we are on 4 we can select 1,5,7 normally we should also not select 5 again
//
// This should work for random numbers
// If you want to loop that path always loop num not the length of the path
num = random(47);
for(int i = 0; i < num; i++){
if (i == 0) path[i] = 1;
path[i] = random(path[i], 1);
}
pinMode(rot,OUTPUT);
pinMode(schwarz,OUTPUT);
pinMode(reset,INPUT);
Serial.begin(9600);
Wire.begin(); // wake up I2C bus
}
void flipRot() {
digitalWrite(rot,HIGH);
digitalWrite(schwarz,LOW);
delay(10);
digitalWrite(rot,LOW);
}
void flipSchwarz() {
digitalWrite(schwarz,HIGH);
digitalWrite(rot,LOW);
delay(10);
digitalWrite(schwarz,LOW);
}
void flicker() {
for (int i=0; i <= 5; i++){
flipSchwarz();
delay(100);
flipRot();
delay(100);
}
}
void getCurrentMove() {
Wire.beginTransmission(0x20);
Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
if (inputs>0) // if a button was pressed
{
for(int i = 0; i < 8; i++){
// See http://arduino.cc/en/Reference/BitRead
// should work. not tested
if(bitRead(inputs, i) == 1){
// do something with the bit
// you can do something like
if(i == 0) doSomething();
}
}
// please write comments in english
// gibt current_move einen Wert zwischen 0 und 7, entsprechend dem letzten aktivierten Schalter.
}
else
{
Wire.beginTransmission(0x20);
Wire.write(0x13); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
if (inputs>0) // if a button was pressed
{
// please write comments in english
// gibt current_move einen Wert zwischen 8 und 15, entsprechend dem letzten aktivierten Schalter.
}
else
{
Wire.beginTransmission(0x21);
Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address
Wire.endTransmission();
Wire.requestFrom(0x21, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
if (inputs>0) // if a button was pressed
{
// please write comments in english
// gibt current_move einen Wert zwischen 16 und 23, entsprechend dem letzten aktivierten Schalter.
}
else
{
// ...
}
}
}
}
void loop() {
getCurrentMove();
if (current_move == path[step])
{
flipSchwarz();
step = step+1;
}
else if (((step >= 1) && (current_move == path[step-1])) || ((step >= 2) && (current_move == path[step-2])))
{
if (step > num)
{
flicker();
}
}
else
{
flipRot();
step = 0;
}
}
void doSomething(){
Serial.println("o_O");
}