Hi Guys,
I'm very new to programming and arduino and I'm learning off the examples, youtube, books etc.
At the moment I'm trying to build a custom controller for a device I have.
So, lets say I got two row of switches, each with two switch and every switch has a LED.
I can flick between LEDs in each individual rows. Now I want to add a fifth button which flicks between the active LEDs in each row.
Lets say:
In row one I got LED 2 on
In row two I got LED 1 on
I want to have a switch that when I press turns Row1,LED1 ON. Row2,LED2 ON and all others off.
If you know the concept of vision mixers row 1 is my programme and row 2 is my preview and the fifth switch is the "take" button (and thats what I'm making!)
#define DEBOUNCE 10 // Debaounse tims
#define NUMBUTTONS 4 // Number of toal buttons in the console
// BUTTONS
byte buttons [] = {2, 3, 4, 5};
// LEDS
byte ledPin [] = {8, 9, 10, 11};
#define NUMBUTTONS sizeof(buttons)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
void setup() {
byte i;
Serial.begin(9600);
Serial.print("Console V1 with ");
Serial.print(NUMBUTTONS, DEC);
Serial.println(" buttons");
// pin13 LED
pinMode(13, OUTPUT);
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
pinMode(ledPin[i], OUTPUT);
}
}
void loop() {
byte thisSwitch=thisSwitch_justPressed();
switch(thisSwitch)
{
case 0:
setLedStatus(ledPin[0],1,0);break;
case 1:
setLedStatus(ledPin[1],2,0); break;
case 2:
setLedStatus(ledPin[2],3,2); break;
case 3:
setLedStatus(ledPin[3],4,2); break;
}
}
void check_switches(){
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
byte previousswitch;
if (millis() < lasttime) {
// we wrapped around, lets just try again
lasttime = millis();
}
if ((lasttime + DEBOUNCE) > millis()) {
// not enough time has passed to debounce
return;
}
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++) {
justpressed[index] = 0; //when we start, we clear out the "just" indicators
justreleased[index] = 0;
currentstate[index] = digitalRead(buttons[index]); //read the button
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
previousswitch = justpressed[index];
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
justreleased[index] = 1; // just released
}
pressed[index] = !currentstate[index];
}
previousstate[index] = currentstate[index];
}
}
byte thisSwitch_justPressed() {
byte thisSwitch = 255;
check_switches(); //check the switches & get the current state
for (byte i = 0; i < NUMBUTTONS; i++) {
current_keystate[i]=justpressed[i];
if (current_keystate[i] != previous_keystate[i]) {
if (current_keystate[i]) thisSwitch=i;
}
previous_keystate[i]=current_keystate[i];
}
// r-> last_pressed = "s1";
return thisSwitch;
}
byte setLedStatus(byte activeLed, byte activeButton, byte activeRow){ // This function sets the active switch's LED on, and turn the other ones off
for (int thisPin = 0; thisPin < NUMBUTTONS/2; thisPin++) {
digitalWrite(ledPin[thisPin+activeRow], LOW);
}
digitalWrite(activeLed, HIGH);
Serial.print("switch ");
Serial.print(activeButton);
Serial.println(" just pressed");
}
This code worked fine I just have no Idea how to add the functionality that I need to it!
Many thanks,
Sep