Hello,
I’m a complete noob at programming. I have an arduinoUno.
I need help with a project I’m working on which is basically recording a sequence of push buttons to automate something.
I have 12 pushbuttons going into analog A0 and I’m using a resistor chain to read a voltage difference between button 1 and button 12.
The code I have is based on this site:
I can get an accurate analogRead from every button, but I hit a wall with recording.
I set a counter to see how many times A0 has been pressed.
I also set a value for each of the 12 pushButtons in an int buttonValue.
My question is the following:
I want to be able to record this in an array, myArray[counter,buttonValue];
and then have it play back in the correct order.
For now I will be using LEDs on 12 digital outputs without the serial monitor to see if it works, but I
just can’t seem to get my head around recording which button was pressed, along with the counter, and if possible I’d also like to record how long a button has been pressed.
Then send the recorded information to each one of the 12 digital outputs in the order they were recorded.
I’ve been looking at 2D arrays but I still can’t figure out how to do what I need.
Unfortunately the code I have only deals with reading pushbuttons and assigning them a value. The code is simplified down to two buttons, and the recording and playback buttons are on digitalInputs, and two LEDs on digitalOutputs. Eventually the recButton and playButton will be on A1 and A2, so I have enough room for the 12 solenoids on the digital pins.
I only need to have two solenoids on at the same time (max) and I will be using an external power supply and some transistors (or whatever you guys suggest).
I’d love to know if this is possible. Thanks.
// these constants won't change:
const int buttonPin = 0; // the pin that the pushbutton is attached to
const int ledPin1 = 4; // the pin that the LED is attached to
const int ledPin2 = 5;
const int RECBUTTON = 7;
const int PLAYBUTTON = 8;
const int BUTTON1 = 1;
const int BUTTON2 = 2;
const int BUTTON1LOW = 1000;
const int BUTTON1HIGH = 1024;
const int BUTTON2LOW = 900;
const int BUTTON2HIGH = 950;
// this bunch of code manages the counter, recording, and playback for the buttons that lead to the solenoids
int lastButtonState = 0;
int lastRecState = 0;
int lastPlayState = 0;
int buttonCount = 0;
int buttonValue = 0;
bool isRec = false;
bool isPlay = false;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
pinMode(RECBUTTON, INPUT);
pinMode(PLAYBUTTON, INPUT);
// initialize the LED as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
int reading = analogRead(buttonPin);
int play = digitalRead(PLAYBUTTON);
int rec = digitalRead(RECBUTTON);
int tmpButtonState = LOW;
// Serial.println(reading);
// compare the buttonState to its previous state
if (reading != lastButtonState) {
// if the state has changed, increment the counter
if (reading > 1) {
buttonCount++;
Serial.println("on");
Serial.print("counter: ");
Serial.println(buttonCount);
}
if (reading > BUTTON1LOW && reading < BUTTON1HIGH){
tmpButtonState = BUTTON1;
buttonValue = 1;
Serial.println(buttonValue);
}
else if (reading > BUTTON2LOW && reading < BUTTON2HIGH){
tmpButtonState = BUTTON2;
buttonValue = 2;
Serial.println(buttonValue);
}
else
{
tmpButtonState = LOW;
}
// if (reading > 100)Serial.println(buttonValue);
// Delay a little bit to avoid bouncing
delay(100);
}
// save the current state as the last state,//
//for next time through the loop
lastButtonState = reading;
if(buttonCount>=20)buttonCount=0;
}