Hi there, I'd love some help or advice with my project as it's buggy.
I've set up an Arduino Uno with an Ethernet shield to a 4x8 segment display board (running on a MAX7219 chip). This is rigged up to a reception bell with a switch. The idea is every time the bell is rung, it increments up the count on the 8 seg counters and also sends out a random tweet with that number attached. It's plugged into a 9V adaptor from the mains and the ethernet cable running to my BE router.
You can see the twitter account, with some images of the set up here:
https://twitter.com/#!/little_winsIt all seems to be working fine, however, it's sometimes sending out random tweets (about 3 a day) and counting up (i.e. like the bell's been rung, but I know it hasn't been). At first I thought this might be a loose connection in the switch circuit, but after a visual inspection I'm not sure. Might it be some sort of interference in the arduino board from the mains supply or the ethernet cable... or something else?
My code is below (if that helps). Any advice gratefully appreciated!
#include "LedControl.h" // include the library for MAX72XX
#include <avr/pgmspace.h>
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
LedControl lc=LedControl(7,6,5,1); //Set the pins for the MAX7219 chip
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 7 is connected to the DataIn
pin 6 is connected to the CLK
pin 5 is connected to LOAD
We have only a single MAX72XX.
*/
int buttonPressCount;
const int buttonPin = 2; //the pin that the pushbutton is attached to
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 button
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 2, 250 };
// Your Token to Tweet
Twitter twitter("Mytwittertokengoeshere");
// Message to post
char msg[64];
prog_char string_0[] PROGMEM = "Hooray! Another little "; // "String 0" etc are strings to store - change to suit.
prog_char string_1[] PROGMEM = "Bada-bing!";
prog_char string_2[] PROGMEM = "High fives at King's St: ";
prog_char string_3[] PROGMEM = ":-) ";
prog_char string_4[] PROGMEM = "Happy 'cos I just had a little";
prog_char string_5[] PROGMEM = "A small, good thing just happened:";
prog_char string_6[] PROGMEM = "W00t! ";
prog_char string_7[] PROGMEM = "Ah, that's nice ";
prog_char string_8[] PROGMEM = "Yippee for ";
// Then set up a table to refer to your strings
PROGMEM const char *string_table[] = // change "string_table" name to suit
{
string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8 };
char buffer[64]; // make sure this is large enough for the largest string it must hold
long randNumber;
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
pinMode(buttonPin, INPUT); //initialize the button pin as a input
delay(100);
Serial.begin(9600);
Serial.println("I am connecting ...");
randomSeed(analogRead(0));
Ethernet.begin(mac);
}
void loop(){
buttonState = digitalRead(buttonPin); //read the pushbutton input pin
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
Serial.println("button pressed");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
printNumber(buttonPushCounter); //function to update the display 'requires button press count'
delay(100);
}
/* Using the string table in program memory requires the use of special functions to retrieve the data.
The strcpy_P function copies a string from program space to a string in RAM ("buffer").
Make sure your receiving string in RAM is large enough to hold whatever
you are retrieving from program space.
*/
int i = random(9);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
snprintf (msg, 64, "%s #win no: %d", buffer, 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.");
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
}
void printNumber(int buttonPushCounter)
{
int ones;
int tens;
int hundreds;
int thousands;
boolean zero;
if(buttonPushCounter < 0 || buttonPushCounter > 9999)
return;
ones=buttonPushCounter%10;
buttonPushCounter=buttonPushCounter/10;
tens=buttonPushCounter%10;
buttonPushCounter=buttonPushCounter/10;
hundreds=buttonPushCounter%10;
buttonPushCounter=buttonPushCounter/10;
thousands=buttonPushCounter%10;
buttonPushCounter=buttonPushCounter/10;
lc.setDigit(0,3,(byte)thousands,false);
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
delay(250); //this will make it so you don't get double counts.
}