I am trying to make my beginner project - neater. Currently, I have 4 buttons connected to my breadboard, and I am doing things in a bad way. No millis() etc, and a lot of cut and paste in my loop. My first refactor, is to move the 'logic' code into a function, that I call, passing in parameters for each button.
My loops, currently, looks like this:
void loop() {
int pinAutopilotValue = digitalRead(AUTOPILOT_PIN);
int pinHeadingValue = digitalRead(HEADING_PIN);
int pinAltValue = digitalRead(ALT_PIN);
int pinFLCValue = digitalRead(FLC_PIN);
delay(10);
if(buttonAutopilotStatus != pinAutopilotValue) {
buttonAutopilotStatus = pinAutopilotValue;
Serial.println(buttonAutopilotStatus);
if(buttonAutopilotStatus == 0) {
digitalWrite(13,HIGH);
Keyboard.press('z');
delay(50);
Keyboard.releaseAll();
} else {
digitalWrite(13,LOW);
}
}
if(buttonHeadingStatus != pinHeadingValue) {
buttonHeadingStatus = pinHeadingValue;
Serial.println(buttonHeadingStatus);
if(buttonHeadingStatus == 0) {
digitalWrite(13,HIGH);
Keyboard.press('h');
delay(50);
Keyboard.releaseAll();
} else {
digitalWrite(13,LOW);
}
}
if(buttonAltStatus != pinAltValue) {
buttonAltStatus = pinAltValue;
Serial.println(buttonAltStatus);
if(buttonAltStatus == 0) {
digitalWrite(13,HIGH);
Keyboard.press('a');
delay(50);
Keyboard.releaseAll();
} else {
digitalWrite(13,LOW);
}
}
if(buttonFLCStatus != pinFLCValue) {
buttonFLCStatus = pinFLCValue;
Serial.println(buttonAltStatus);
if(buttonFLCStatus == 0) {
digitalWrite(13,HIGH);
Keyboard.press('f');
delay(50);
Keyboard.releaseAll();
} else {
digitalWrite(13,LOW);
}
}
}
Poor, I know, but before you vomit - I'm refactoring. My idea is to move the logic into a single function:
void checkButtonState(int pinNumber, int currentState) {
if(buttonFLCStatus != pinFLCValue) {
buttonFLCStatus = pinFLCValue;
Serial.println(buttonAltStatus);
if(buttonFLCStatus == 0) {
digitalWrite(13,HIGH);
Keyboard.press('f');
delay(50);
Keyboard.releaseAll();
} else {
digitalWrite(13,LOW);
}
}
}
(with the right code though)
And then, in my loop, itterate through an array of object, calling the function. (Next, I will try interrupts, but... baby steps).
But, I'm stuck early. I am trying to create an array of buttons. With a button being a class of properties I need.
What I am trying is:
#include <Keyboard.h>
const int AUTOPILOT_PIN = 2;
const int HEADING_PIN = 3;
const int ALT_PIN = 4;
const int FLC_PIN = 5;
class Button {
public:
int pinNumber;
int currentStatus = 0;
};
Button buttonArray[] = new Button[4];
void setup() {
....
However, I'm struck down with the error:
Switch:14:24: error: initializer fails to determine size of 'buttonArray'
** Button buttonArray[] = new Button[4];**
** ^~~~~~~~~~~~~**
C:\Users\craig\Documents\Arduino\Switch\Switch.ino:14:24: warning: array must be initialized with a brace-enclosed initializer [-fpermissive]
Switch:14:24: error: conversion from 'Button*' to non-scalar type 'Button' requested
exit status 1
initializer fails to determine size of 'buttonArray'
If anyone can assist me with this extremely basic thing, it would be much appreciated.