Hi,
Before all you wiser people LOL at my attempt, as you may well do so. I’m a novice, but I’ve made an attempt, made the effort, tried, failed and got myself confused.
I have an Arduino Due with 9 momentary switches to Gnd and digital input pins 2,3,4,5,6,7,8,9 & 10. I wish to emulate keyboard presses to control a DAW / VST.
As you will see I have got myself very confused with my lack of coding skills and knowledge of arrays.
Hear is the sketch I’ve attempted, please don’t LOL to much;
/*
Keyboard Message test
For the Arduino Leonardo and Micro.
Sends a text string when a button is pressed.
The circuit:
pushbutton attached from pin 4 to +5V
10-kilohm resistor attached from pin 4 to ground
created 24 Oct 2011
modified 27 Mar 2012
by Tom Igoe
modified 11 Nov 2013
by Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/KeyboardMessage
*/
#include "Keyboard.h"
int buttonPins[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int previousButtonState = LOW; // for checking the state of a pushButton
void setup()
{
for (int i = 0; i < 9; i++)
pinMode(buttonPins[i], INPUT_PULLUP);
Keyboard.begin(); // initialize control over the keyboard:
}
void loop()
{
for (int i = 0; i < 9; i++)
int buttonState = digitalRead(buttonPins[i]); // read the pushbutton
if ((buttonState != previousButtonState) // if the button state has changed
&& (buttonState == LOW)) // and it's currently pressed
{
switch()
{
case 1:
Keyboard.write(' '); // press spacebar
break;
case 2:
Keyboard.write('a');
break;
case 3:
Keyboard.write('b');
break;
case 4:
Keyboard.write('c');
break;
case 5:
Keyboard.write('d');
break;
case 6:
Keyboard.write('e');
break;
case 7:
Keyboard.write('f');
break;
case 8:
Keyboard.write('g');
break;
case 9:
Keyboard.write('h');
break;
}
previousButtonState = buttonState; // save the current button state for comparison next time
}
}
And hear are the error msgs I currently get;
Arduino: 1.8.1 (Mac OS X), Board: "Arduino Due (Programming Port)"
/Users/darrensimms/Documents/Arduino/a_b_c/a_b_c.ino: In function 'void loop()':
a_b_c:38: error: 'buttonState' was not declared in this scope
if ((buttonState != previousButtonState) // if the button state has changed
^
a_b_c:41: error: expected primary-expression before ')' token
switch()
^
exit status 1
'buttonState' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I had thought of using ‘else if’, instead of ‘case 1 etc’, but on reading, theres nothing to be gained by doing so.
So I ask for some pointers to my confusion and errors please, when you’ve all finished laughing.
Dizzwold.