I am trying to write program to record button presses. That is - when you press buttons my program can play back the buttons you pressed in the exact sequence you pressed the. my array is onTimes [1000][3]. the first column will record the button, second column will record millis() and the third column will record previous millis(). However something is wrong with my logic. I can not even see see first value of onTimes*[0] in the serial monitor. I have not written the rest of the program, just working on record part now.*
void loop()* {
if (digitalRead(record) == LOW)*
{*
onRec = true;*
}*
while (onRec == true)*
{*
long buttons = myInputs.getButtons();*
if (buttons != prevButton)*
{*
int i;*
_ onTimes*[0] = buttons;_ _ onTimes[1] = millis(); if(i != 0) { onTimes[i-1][2] = onTimes[1]; } buttons = prevButton; i++; Serial.println(onTimes[0]); Serial.println(buttons); } } }_ _the long buttons = myInputs.getButtons(); works with serial.println(buttons) but everything else in between does not (2nd if statement down to Serial.println(onTimes[0]). any suggestions would be greatly apprciated!*_
The Serial.print statements to print the data for any given value of i come AFTER you have incremented i. Try printing the values BEFORE you increment i.
First off please post you code between the brackets you get with the hash key (#) in the reply box.
Second:- onTimes [1000][3] requires more memory than most arduinos have. In order to store the millis() you need a long int, that's 4 bytes so 410003 gives you a requirement of 12,000 bytes.
Third I am not sure what the variable buttons represents as you haven't shown the myInputs.getButtons(); code. But you define i and an int, don't give it a value, and then use it to address an array!
Then you compare it to 0 (still not defined), then increment it. Next time you go to the int i it wipes out the value again.
I am using an atmega168-20PU. The data sheet says that it has 16K program memory.
I have the buttons wired to a chip and the long buttons = myInputs.getButtons(); code is written such that when you press the first button it tells the atmega 1, if you press the second button it tells the atmega 10. Basically the chip I'm uing is a multiplexer. Each pin on the multiplexer is a button. If I press buttons/pins 3 and 5 the arduino will receive 10100. the lsb is button#1 and msb is button#2. I did not write this header but it works because I used it alone with just the Serial.println(buttons); and I coul see the correct response in the serial monitor (but not in binary).
I'm not sure exactly what you mean by"you define i and an int, don't give it a value, and then use it to address an array!
Then you compare it to 0 (still not defined), then increment it. Next time you go to the int i it wipes out the value again." Could you elaborate on this comment, my programming experience is not so great. Thanks a lot for your time!
here's the code I have now:
/*****************************************
* Initialize Pins *
*****************************************/
int shiftOutLatch = 2; //Latch pin for FET595 PCBs (output)
unsigned long shiftOutClk = 3; //Clock pin for FET595 PCBs (output)
int shiftOutData = 4; //Data pin for FET595 PCBs (output)
//int shiftInLatch = 5; //Latch pin for 165 PCBs (input)
//unsigned long shiftInClk = 6; //Clock for pin 165 PCBs (input)
//int shiftInData = 7; //Data pin for 165 PCBs (input)
byte record = 8; //Record button
boolean onRec = false;
byte play = 9; //play button
byte reset = 10; //Reset button
long onTimes[1000][3]; //Array movements (1000 possiblities) by times [3 columns]
int prevButton = 0;
shiftInput myInputs = shiftInput(7,6,5,8); // (dataPin,clockPin,latchPin,number of inputs from 165)
/*****************************************
* Setup *
*****************************************/
void setup()
{
pinMode(shiftOutLatch, OUTPUT);
pinMode(shiftOutClk, OUTPUT);
pinMode(shiftOutData, OUTPUT);
// pinMode(shiftInLatch, OUTPUT);
// pinMode(shiftInClk, OUTPUT);
pinMode(record, INPUT);
pinMode(play, INPUT);
pinMode(reset, INPUT);
Serial.begin(9600);
}
/*****************************************
* Main Loop *
*****************************************/
void loop()
{
int i=0;
if (digitalRead(record) == LOW)
{
onRec = true;
}
while (onRec == true)
{
long buttons = myInputs.getButtons();
if (buttons != prevButton)
{
onTimes[i][0] = buttons;
onTimes[i][1] = millis();
if(i != 0)
{
onTimes[i-1][2] = onTimes[i][1];
}
buttons = prevButton;
Serial.println(onTimes[i][0]);
Serial.println(buttons);
i++;
}
}
}
I'm not sure exactly what you mean by"you define i and an int, don't give it a value, and then use it to address an array!
Then you compare it to 0 (still not defined), then increment it. Next time you go to the int i it wipes out the value again." Could you elaborate on this comment, my programming experience is not so great.
I think he meant that "you define i AS an int". You did, but you did not give it a value. Different compilers perform different actions with uninitialized variables. Most will give the variable a reasonable value (0 for ints, floats, and doubles, or NULL for char), but not all. I worked on a system that defaulted to 3087 for uninitialized ints. Made it easy to detect when you failed to initialize something.
You have fixed that problem, I see.
You were, and still are, using i as an index into an array. Since the default value on the Arduino, for an int, is 0, you were able to get away with that. Had you been working on my old system, the default value would have been beyond the end of the array you defined.
The final part of his statement about wiping out the value of i referred to the fact that the "int i;" statement was inside the while loop. Each time the loop repeats, a new i is used. Again, you have corrected that problem by moving the declaration outside of the while block.
you guys are right i didn't realize that. I got it to work with [10]3. I'll rethink it. Maybe I'll use external sram or something. Thanks for your time! I'll let you know what I come up with. :-/