Read, store and compare game sequence?


i'm working on simple gamebox. hardware point there 4 buttons (n,e,s,w) , rgb led on box. press buttons in right order 11 times 'win', else loose after pressing 11 times. box shuts down after 11 buttons pressed. no indication if correct button pressed until 11 have been chosen , fixed sequence. should extremely simple, thought! lost. best code this? suggestions appreciated. mechanism picking choice (=what button pressed), store choice, , how compare sequence @ end gives me trouble...

(the led used on indication (g) , confirmation button pressed (g+b). if 11 buttons pressed in right order deliver code thru ledb)


code thinking until ran out of ideas:

code: [select]

#include <lowpower.h>


int ledr = 2; //the pin of red led.
int ledg = 3; //the pin of green led.
int ledb = 4; //the pin of blue led.
int buttonpinn = 5;
int buttonpine = 6;
int buttonpins = 7;
int buttompinw = 8;
int done = 0;
int action = 0;

boolean old_valn = low;
boolean old_vale = low;
boolean old_vals = low;
boolean old_valw = low;

int pause = 1000;
long lastmove = millis();

void setup()
{
  pinmode(ledr, output);
  pinmode(ledg, output);
  pinmode(ledb, output);
  pinmode(buttonpinn, input_pullup);           // set pin input
  pinmode(buttonpine, input_pullup);           // set pin input
  pinmode(buttonpins, input_pullup);           // set pin input
  pinmode(buttonpinw, input_pullup);           // set pin input
  digitalwrite(ledg, high);
  delay(1000);
  digitalwrite(ledg,low);
  delay(1000);
  digitalwrite(ledg,high);
  delay(1000);
  digitalwrite(ledg,low);
  delay(1000);
  digitalwrite(ledg,high);
  delay(100);


 
  //start new game.
  // newgame();
}

void loop()
{

  if(digitalread(buttonpinn) == high && old_val == low)
  {}
    if(digitalread(buttonpine) == high && old_val == low)
  {}
    if(digitalread(buttonpins) == high && old_val == low)
  {}
    if(digitalread(buttonpinw) == high && old_val == low)
  {}


old_valn = digitalread(buttonpin);
old_vale = digitalread(buttonpin);
old_vals = digitalread(buttonpin);
old_valw = digitalread(buttonpin);

quote
how compare sequence @ end gives me trouble...
don't @ end, go along.  put 4 button pin numbers in array , correct sequence of eleven button pin numbers in second array. 

set boolean variable true start reading inputs in loop working through button pin array.  when detect button has become pressed compare current pin number in second array.  if pin numbers don't match set boolean false.  whether or not guess correct increment index second array.

when reach end of second array examine boolean.  if true sequence correct else wrong.


Arduino Forum > Using Arduino > Programming Questions > Read, store and compare game sequence?


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor