Storing instructions and preforming things based on them

Hello. I have Arduino Duemilanove Atmega 168, 5xLED with different colors, 6x10k ohm resistors, 6xpush-button and I have connected them like in Button example (http://arduino.cc/en/Tutorial/Button) and my additional 5 push-buttons are connected to pins 3, 4, 5, 6, 7. Now I would like Arduino , when turned ON, to detect how many times was each of the first 5 buttons pressed and in which order and then when the sixth is pressed it would blink each LED according to the number of the pressed button and how many times that buttons was pressed. For instance i press: 4 - 3 - 3 - 5 - 5 - 5 - 2 - 1 - 6. Then it would have to blink fourth LED once, third LED twice, fifth LED three times, second LED once and first LED once and once it has done that delete these instructions and wait for new set of instructions. I had in mind to try and do it using arrays but I don't know in which memory to store it since EEPROM has 100 000 limit and I can't modify FLASH once the sketch is uploaded and I don't have any additional storage. Any help or advice would be deeply appreciated.

Do some research on arrays. It sounds as though they would meet your needs to store short sequences and to replay them.

since EEPROM has 100 000 limit

That is only a problem for fast loops writing data. Calculate how long this would be if you only changed your combination every 10 seconds.

Grumpy_Mike:

since EEPROM has 100 000 limit

That is only a problem for fast loops writing data. Calculate how long this would be if you only changed your combination every 10 seconds.

Lets see:

  • 100,000/6 = 16,666.7 minutes;
  • 16666.7/60 = 277.8 hours;
  • 277.8/24 = 11.6 days;
  • 11.6/7 = 1.7 weeks.

Here is my first code for only two LEDs and two push-butoons and without the button which tells Arduino that inputting/pressing buttons is over, but it dosen’t work :

int const button1 = 2;
int const button2 = 3;
int const led1 = 8; 
int const led2 = 9;
int pressedbuttons [10];
int counter = 0;
int val1 = 0;
int val2 = 0;

void setup () {
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
}

void loop () {
  while (counter < 10) {

    if ((val1 = digitalRead(button1)) == HIGH) {
      pressedbuttons[counter] = 1;
      while (val1 == HIGH) {};
      counter++;
    }
    
    if ((val2 = digitalRead(button2)) == HIGH) {
      pressedbuttons[counter] = 2;
      while (val2 == HIGH) {};
      counter++;
    }
    
  }

  for (int i = 0; i < 10; i++) {
    if (pressedbuttons[i] == 1) {
      digitalWrite (led1, HIGH);
      delay (100);
      digitalWrite (led1, LOW);
    } else if (pressedbuttons[i] == 2) {
      digitalWrite (led2, HIGH);
      delay (100);
      digitalWrite (led2, LOW);
    }
  }
}
      while (val1 == HIGH) {};

val1 is not bound to the digitalRead() statement. If val1 is HIGH when this loop starts, the loop will never end. Surely that is not what you want.

If I put:

while ((val1 = digitalRead(button1)) == HIGH)

it also won't work. Could you give me some suggestion?

Why do you need the while loop after storing the number of the pressed button?

You can extend the use of arrays in your program to hold button and LED pin numbers. This will make it much easier to add more buttons and LEDs. Something like this (untested)

byte buttonPins[] = {2, 3};
byte ledPins[] = {8, 9};
byte pressedButtons [10];
byte counter = 0;
const byte numberOfButtons = 2; 
const byte numberOfReads = 10;

void setup () 
{
  for (byte button = 0; button < numberOfButtons; button++)
  {
    pinMode (buttonPins[button], INPUT);
    pinMode (ledPins[button], OUTPUT);
  }
}

void loop () 
{
  while (counter < numberOfReads) 
  {
    for (byte button = 0; button < numberOfButtons; button++)
    {
      if (digitalRead(buttonPins[button]) == HIGH) 
      {
        pressedButtons[counter] = button;
        counter++;
      }
    }

    for (byte i = 0; i < numberOfReads; i++) 
    {
      digitalWrite(pressedButtons[i], HIGH);
      delay (100);
      digitalWrite(pressedButtons[i], LOW);
    } 
  }
}

Why are you claiming that your Arduino has insufficient memory for this simple problem ?

Are you aware that, in addition to the flash memory ( where the program code goes ), and the eeprom ( where you might want to store numbers that you need to keep ), are you aware that the arduino has the third kind of memory, the SRAM, where any variables which you use in your program go ? Not much SRAM compared to a proper computer, mind you, but more than enough for your requirement.

You want to take actions when a switch BECOMES pressed, not when the switch IS pressed. Look at the state change detection example, to learn how to do that.

PaulS:
You want to take actions when a switch BECOMES pressed, not when the switch IS pressed. Look at the state change detection example, to learn how to do that.

Thank you for pint that out, but how can I combine that with which button was pressed in which order because that is my goal to make Arduino remember/detect in which order and how many times was each button pressed and then move the car according to that.

but how can I combine that with which button was pressed

Determining when a switch BECOMES pressed is not a function of how many switches there are. At any given time, a given switch is pressed this time and was not pressed last time (it BECAME pressed), is pressed this time and was pressed last time, is not pressed now but was pressed before (it BECAME released), or is not pressed now and was nor pressed before.

Therefore, on any given check of the switches, you can determine which one(s) BECAME pressed, which one(s) BECAME released, and which one(s) had no change of state.

in which order

The only way to determine order is to check often.

This would be code to check if any of the two buttons has changed its state and how many times, but I still don't know how to make it to remember in which order the were pressed:

const int  buttonPin1 = 2; 
const int buttonPin2 = 3;

int buttonPushCounter1 = 0;   
int buttonState1 = 0;         
int lastButtonState1 = 0;     

int buttonPushCounter2 = 0;   
int buttonState2 = 0;         
int lastButtonState2 = 0;     

void setup() {
  pinMode (buttonPin1, INPUT);
  pinMode (buttonPin2, INPUT);
}


void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 != lastButtonState1) {
    if (buttonState1 == HIGH) {
      buttonPushCounter1++;
    }
  }  
  lastButtonState1 = buttonState1;


  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      buttonPushCounter2++;
    }
  }  
  lastButtonState2 = buttonState2;
}

but I still don't know how to make it to remember in which order the were pressed:

Remember the order where? For how long? The millis() function will tell you how long the Arduino has been running. You can record when each switch becomes pressed, and use that to determine which one became pressed first.

PaulS:

but I still don't know how to make it to remember in which order the were pressed:

Remember the order where? For how long? The millis() function will tell you how long the Arduino has been running. You can record when each switch becomes pressed, and use that to determine which one became pressed first.

My idea was this: the sketch should be sensoring 6 buttons (LEFT, RIGHT, FORWARD, BACKWARD, GO, RESET), when sombedoy starts pressing the buttons for direction it should remember the order and how many times each button was pressed. I was thinking of storing it into array. Then when the "GO" button is pressed it should move in the directions according to the pressed buttons. When it is done it should stop and when I press "RESET" button it should delete these instructions and wait for new ones.

Here I have tried experimenting with 2 LED’s and 3 pushbuttons, but it also wouldn’t work:

int const button1 = 2;
int const button2 = 3;
int const STOP = 3;
int const led1 = 8;
int const led2 = 9;
int val = 0;
int array [10];
int buttonPushCounter1 = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonPushCounter2 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int counter = 0;

void setup () {
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
  pinMode (STOP, INPUT);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
}

void loop () {
  val = digitalRead (STOP);
  
  while (val == LOW) {
    buttonState1 = digitalRead (button1);
    buttonState2 = digitalRead (button2);
    
    if (buttonState1 != lastButtonState1) {
      if (buttonState1 == HIGH) {
        buttonPushCounter1++;
        array[counter] = 1;
        counter++;
      } 
    }
    lastButtonState1 = buttonState1;
    
    if (buttonState2 != lastButtonState2) {
      if (buttonState2 == HIGH) {
        buttonPushCounter2++;
        array[counter] = 2;
        counter++;
      }
    }
  }
  
  for (int i = 0; i < 10; i++) {
    if (array[i] == 1) {
      digitalWrite (led1, HIGH);
      delay(1000);
      digitalWrite (led1, LOW);
      delay(1000);
    } else if (array[i] == 2) {
      digitalWrite (led2, HIGH);
      delay(100);
      digitalWrite (led2, LOW);
      delay(100);
    }
  }
}

when sombedoy starts pressing the buttons for direction it should remember the order and how many times each button was pressed. I was thinking of storing it into array.

Does the time between presses matter? I would think it would.

How much SRAM do you have available? What do you need to store? Which switch was pressed, and when, I would think. That's a byte for the switch number and 4 bytes for the time. Divide the amount of memory you have available by 5 to get the number of switch press/time sets you can save in two arrays. Save the data in the arrays.

PaulS:

when sombedoy starts pressing the buttons for direction it should remember the order and how many times each button was pressed. I was thinking of storing it into array.

Does the time between presses matter? I would think it would.

How much SRAM do you have available? What do you need to store? Which switch was pressed, and when, I would think. That's a byte for the switch number and 4 bytes for the time. Divide the amount of memory you have available by 5 to get the number of switch press/time sets you can save in two arrays. Save the data in the arrays.

I have Arduino Duemilanove Atmega 168, so 1024 bytes of SRAM.