Button Control
int slidervolumes[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
// incomplete code.
//instructions: pass pin number, up key, down key, to read_slider, for example
read_slider(0, 't', 'y');
read_slider(1, 'g', 'h');
read_slider(2, 'b', 'n');
read_slider(3, KEY_DOWN_ARROW, KEY_UP_ARROW);
read_slider(4, KEY_RIGHT_ARROW, KEY_LEFT_ARROW);
//pass pin number, highkey, lowkey to control button
//for example
control_button(2, 'q', 'w');
control_button(3, 'e', 'r');
control_button(4, 'a', 's');
control_button(5, 'd', 'f');
control_button(6, 'z', 'x');
control_button(7, 'c', 'v');
control_button(8, 'o', 'p');
// this should all be done in loop
}
void read_slider(int slider_number, char upkey, char downkey) {
int val = analogRead(slider_number); //Read slider value from analog 0
int newvolume = map(val, 0, 1023, 0, 27); //map slider value to [0,2]
if (newvolume != slidervolumes[slider_number]) {
Serial.println("volume changed!");
control_slider(slider_number, slidervolumes[slider_number], newvolume, upkey, downkey);
slidervolumes[slider_number] = newvolume; //change old value for comparison next time
}
}
void control_slider(int slider_number, int original_vol, int new_vol, char upkey, char downkey) {
int keypress = original_vol - new_vol; //we get the num differences between the two tracks
if (keypress < 0) {
for (int i = 0; i < abs(keypress); i++) {
Keyboard.write(downkey);
}
} else {
for (int i = 0; i < abs(keypress); i++) {
Keyboard.write(upkey);
}
}
}
void control_button(int button_number, char highkey, char lowkey) {
int button_state = digitalRead(button_number);
if (button_state != lastButtonState[button_number]) {
// if the state has changed, increment the counter
if (button_state == HIGH) {
Serial.println("1");
Keyboard.write(highkey);
delay(500);
}
else {
Keyboard.write(lowkey);
}
//no pressed = 0
//pressed = 1
lastButtonState[button_number] = button_state;
}
}