Hi all,
Im relatively new to C++ although i know my way around...
I have been having some trouble with the following code.
The code is counting the state of D2, de bouncing it (because of reed switch) and then sending the int buttonCounter over serial to the display.
#include <genieArduino.h>
const int buttonPin = 2; // reed pin
const int ledPin = 13; // LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
int buttonCounter = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 2; // the debounce time; increase if the output flickers
//GENIE
Genie genie;
#define RESETLINE 4
void setup() {
Serial.begin(9600); // Serial0 @ 9600 Baud
genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display
Serial.setTimeout(100);
pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
digitalWrite(RESETLINE, 1); // Reset the Display via D4
delay(100);
digitalWrite(RESETLINE, 0); // unReset the Display via D4
delay (3500); //let the display start up after the reset (This is important)
genie.WriteContrast(15);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
digitalWrite(buttonPin, HIGH);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
ledState = !ledState;
buttonCounter++;
genieWrite ();
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
Serial.println(buttonCounter);
//genie.WriteObject(GENIE_OBJ_COOL_GAUGE, 0, buttonCounter); //Talk to the display
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
void genieWrite() {
genie.WriteObject(GENIE_OBJ_COOL_GAUGE, 0, buttonCounter); //Talk to the display
}
the genie.WriteObject line seems to halt all of the other functions, they still work but only very slowly... its almost like there is a massive delay at the end of the main loop...
the information does get to the display aswell, but it is the same as the ledstate in that is is very slow and seems to only trigger if you hit the reed at the right time..?
any ideas?
Mitch