Counting button presses within a switch/case state.
hi all,
forgive poor explanation of problem i'm bad @ programming.
i'm building midi controller, based on due. though has plenty of pins, managed use of them, , had resort little trick:
https://www.instructables.com/id/how-to-access-5-buttons-through-1-arduino-input/
basically using analog input , enough resistors in order have many push buttons need.
in 1 of these button arrays, 1 assigned pin a1, want create submenu, mean following:
the switch has 6 cases, or 6 buttons. each 1 sends midi note , prints different menu on lcd. in 2 cases, able have additional submenu, , change note/lcd every time same button pressed. guess i'm looking state change scenario, have no idea how implement it. appreciated.
here's code, stripped down bare minimum:
forgive poor explanation of problem i'm bad @ programming.
i'm building midi controller, based on due. though has plenty of pins, managed use of them, , had resort little trick:
https://www.instructables.com/id/how-to-access-5-buttons-through-1-arduino-input/
basically using analog input , enough resistors in order have many push buttons need.
in 1 of these button arrays, 1 assigned pin a1, want create submenu, mean following:
the switch has 6 cases, or 6 buttons. each 1 sends midi note , prints different menu on lcd. in 2 cases, able have additional submenu, , change note/lcd every time same button pressed. guess i'm looking state change scenario, have no idea how implement it. appreciated.
here's code, stripped down bare minimum:
code: [select]
#include <midi_controller.h>
#include <midiusb.h>
#include <liquidcrystal.h>
#define velocity 0b01111111
#define latchtime 100
#define speed_multiply 1
#define pulses_per_step 4
#define analog_average 8
const int buttonpin1 = a1;
const int button1 = 1;
const int button2 = 2;
const int button3 = 3;
const int button4 = 4;
const int button5 = 5;
const int button6 = 6;
const int button1low = 970;
const int button1high = 1024;
const int button2low = 750;
const int button2high = 800;
const int button3low = 600;
const int button3high = 660;
const int button4low = 400;
const int button4high = 450;
const int button5low = 270;
const int button5high = 320;
const int button6low = 50;
const int button6high = 100;
// variables change:
int buttonstate1; // current reading input pin
int lastbuttonstate1 = low; // previous reading input pin
// following variables long's because time, measured in miliseconds,
// become bigger number can stored in int.
long lastdebouncetime1 = 0; // last time output pin toggled
long debouncedelay1 = 50; // debounce time; increase if output flickers
// initialize library numbers of interface pins lcd
liquidcrystal lcd(22, 24, 23, 25, 27, 29);
void noteon(byte channel, byte pitch, byte velocity) {
midieventpacket_t noteon = {0x09, 0x90 | channel, pitch, velocity};
midiusb.sendmidi(noteon);
}
void noteoff(byte channel, byte pitch, byte velocity) {
midieventpacket_t noteoff = {0x08, 0x80 | channel, pitch, velocity};
midiusb.sendmidi(noteoff);
}
//_____________________________________________________________________________________________________________________________________________________________________________________________
void setup()
{
//serial.begin(9600);
pinmode(buttonpin1, input);
// set lcd's number of columns , rows:
lcd.begin(20, 2);
// print message lcd.
lcd.setcursor(0, 0); //position cursor
lcd.print(" cont pivot sat ");
lcd.setcursor(0, 1); //position cursor
lcd.print(" hue lum mix ");
usbmidicontroller.setdelay(15); // wait 15 ms after each message not flood connection
delay(1000); // wait second...
}
void controlchange(byte channel, byte control, byte value) {
midieventpacket_t event = {0x0b, 0xb0 | channel, control, value};
midiusb.sendmidi(event);
}
void loop() // refresh inputs
{
// read state of switch local variable:
int reading1 = analogread(buttonpin1);
//serial.println(reading1);
int tmpbuttonstate1 = low; // current reading input pin
if (reading1 > button6low && reading1 < button6high) {
//read switch 6
tmpbuttonstate1 = button6;
} else if (reading1 > button5low && reading1 < button5high) {
//read switch 5
tmpbuttonstate1 = button5;
} else if (reading1 > button4low && reading1 < button4high) {
//read switch 4
tmpbuttonstate1 = button4;
} else if (reading1 > button3low && reading1 < button3high) {
//read switch 3
tmpbuttonstate1 = button3;
} else if (reading1 > button2low && reading1 < button2high) {
//read switch 2
tmpbuttonstate1 = button2;
} else if (reading1 > button1low && reading1 < button1high) {
//read switch 1
tmpbuttonstate1 = button1;
} else {
//no button pressed;
tmpbuttonstate1 = low;
}
// check see if pressed button
// (i.e. input went low buttonstate), and you've waited
// long enough since last press ignore noise:
// if switch changed, due noise or pressing:
if (tmpbuttonstate1 != lastbuttonstate1) {
// reset debouncing timer
lastdebouncetime1 = millis();
}
if ((millis() - lastdebouncetime1) > debouncedelay1) {
// whatever reading at, it's been there longer
// debounce delay, take actual current state:
buttonstate1 = tmpbuttonstate1;
// serial.println(buttonstate);
}
// save reading. next time through loop,
// it'll lastbuttonstate:
lastbuttonstate1 = tmpbuttonstate1;
// set led using state of button testing:
switch (buttonstate1) {
case button1:
lcd.setcursor(0, 0); //position cursor
lcd.print(" cont pivot sat ");
lcd.setcursor(0, 1); //position cursor
lcd.print(" hue lum mix ");
noteon(0, 53, 64);
midiusb.flush();
delay(50);
noteoff(0, 52, 64);
midiusb.flush();
delay(50);
noteoff(0, 48, 64);
midiusb.flush();
delay(50);
noteoff(0, 49, 64);
midiusb.flush();
delay(50);
noteoff(0, 50, 64);
midiusb.flush();
delay(50);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
delay(50);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
break;
case button2:
lcd.setcursor(0, 0); //position cursor
lcd.print("temp tint md");
lcd.setcursor(0, 1); //position cursor
lcd.print("col bst shad hl");
noteon(0, 48, 64);
midiusb.flush();
delay(50);
noteoff(0, 53, 64);
midiusb.flush();
delay(50);
noteoff(0, 52, 64);
midiusb.flush();
delay(50);
noteoff(0, 49, 64);
midiusb.flush();
delay(50);
noteoff(0, 50, 64);
midiusb.flush();
delay(50);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
delay(50);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
break;
case button3:
lcd.setcursor(0, 0); //position cursor
lcd.print("low high ");
lcd.setcursor(0, 1); //position cursor
lcd.print("l.s. h.s. ");
noteon(0, 87, 64); // curves submenu on
midiusb.flush();
delay(20);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
delay(20);
noteoff(0, 53, 64);
midiusb.flush();
delay(20);
noteoff(0, 48, 64);
midiusb.flush();
delay(20);
noteoff(0, 52, 64);
midiusb.flush();
delay(20);
noteoff(0, 50, 64);
midiusb.flush();
delay(20);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
break;
case button4:
lcd.setcursor(0, 0); //position cursor
lcd.print("size pan rotate");
lcd.setcursor(0, 1); //position cursor
lcd.print("aspect tilt opacity");
noteon(0, 50, 64);
midiusb.flush();
delay(50);
noteoff(0, 53, 64);
midiusb.flush();
delay(50);
noteoff(0, 48, 64);
midiusb.flush();
delay(50);
noteoff(0, 52, 64);
midiusb.flush();
delay(50);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
delay(50);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
break;
case button5:
lcd.setcursor(0, 0); //position cursor
lcd.print("radius h/v rat scal");
lcd.setcursor(0, 1); //position cursor
lcd.print("cor s level mix ");
noteon(0, 86, 64); // b-s-m submenu on
midiusb.flush();
delay(20);
noteoff(0, 53, 64);
midiusb.flush();
delay(20);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
delay(20);
noteoff(0, 48, 64);
midiusb.flush();
delay(20);
noteoff(0, 50, 64);
midiusb.flush();
delay(20);
noteoff(0, 52, 64);
midiusb.flush();
delay(20);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
break;
case button6:
lcd.setcursor(0, 0); //position cursor
lcd.print(" pan tilt zoom");
lcd.setcursor(0, 1); //position cursor
lcd.print("rotate width height");
noteon(0, 52, 64);
midiusb.flush();
delay(50);
noteoff(0, 53, 64);
midiusb.flush();
delay(50);
noteoff(0, 48, 64);
midiusb.flush();
delay(50);
noteoff(0, 50, 64);
midiusb.flush();
delay(50);
noteoff(0, 86, 64); // b-s-m submenu off
midiusb.flush();
delay(50);
noteoff(0, 87, 64); // curves submenu off
midiusb.flush();
break;
}
}
Arduino Forum > Using Arduino > Programming Questions > Counting button presses within a switch/case state.
arduino
Comments
Post a Comment