Pages: [1]   Go Down
Author Topic: My project is sending out tweets when it shouldn't...  (Read 1004 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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_wins

It 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!

Code:
#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.
}







Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26195
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How hard is the button pin pulled down?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply - sorry what do you mean by that question?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26195
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code implies that the button pin is connected to +5V when the attached switch is operated, but what is the value of the resistor you have pulling it down to zero volts when it is not operated?
« Last Edit: May 18, 2012, 07:11:41 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I get it. I've got a 10kohm resistor attached to that switch
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is rigged up to a reception bell with a switch.

The logic in the script looks sensible, and if it's basically working I assume you have wired the switch reasonably. It would be a good idea to post a wiring diagram anyway to confirm that. What else is connected to the switch? If the bell is also still connected, I wonder whether something happening on that side is causing enough noise to trigger your input. For example, is it possible the bell is powered by a circuit which has a different earth and drifting slightly relative to the Arduino, or something like that? Or a great long unshielded wire which picks up EM radiation when somebody turns on the megawatt radio transmitter across the street?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Or a great long unshielded wire which picks up EM radiation when somebody turns on the megawatt radio transmitter across the street?

It definitely has this.... hmm, any ideas about how to deal with this?

I'll look to get a wiring diagram up, probably won't be today tho...

Any opinions about whether the 10kohm resistor might be any issue too (pulling down on the switch itself)?

Thanks again for the replies!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26195
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try a lower value resistor, maybe down to 1k.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could make your button pins high, then pull them low to ground with the button.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A ferrite core on the input would help suppress EM induced spikes, and/or you can suppress transient inputs in the software. Getting rid of the interference in the first place would be better, and perhaps dropping the pull-down resistance as AWOL suggests would be your best approach.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: