Hi there,
I want to send out a tweet that includes a number that increments up each time a button is pressed (e.g. tweet no1... tweet no2 etc). I've got the button working so it tweets a character string each time, but I can't for the life of me work out the syntax to include the button press number ('buttonPushCounter' in my code). My code is below, can anyone offer any advice or help?
Many thanks - I'm quite new to this stuff!
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the buttonbyte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 2, 250 };// Your Token to Tweet
Twitter twitter("16902667-I6l74Jz3hrMUz0NKp9rqrK9pqvGSPfeHtOy5at9JM");// Message to post
char msg[ ] = "test tweet number: buttonPushCounter";void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);delay(1000);
//Ethernet.begin(mac, ip);
// or you can use DHCP for autoomatic IP address configuration.
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println("I am connecting ...");
}void loop(){
buttonState = digitalRead(buttonPin);
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {digitalWrite(ledPin, HIGH);
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("button pressed");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);if (twitter.post(msg)) {
int status = twitter.wait();
if (status == 200) {
Serial.println("Tweeted!");
}
else {
Serial.print("failed : code ");
Serial.println(status);
}
}
else {
Serial.println("connection failed.");
}}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;}