Hello. I'm still working on my "tweet that our shop is open" application. Now I'm trying to integrate an LCD display into it to take the place of the serial monitor. For some reason only the first two print statements to the LCD will show but once It runs Ethernet.begin(mac), the code continues to run properly but the print statements to the LCD stop working.
I'm using an Arduino UNO R2 with a Wiznet W5100-compliant ethernet shield and IDE 1.0.1. The LCD display is from SparkFun. It's an ADM2004D 4x20 character display, originally built by Amotec. It's Hitachi HD44780 compatible. The display does work properly with my Arduino when I run the example sketch LiquidCrystal > HelloWorld.
It's odd that I've had so much trouble finding information for the ADM2004D display online. I can't even find it on SparkFun's own site. The closest I can get to it are the ADM2004U and the more similar GDM2004D.
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <EthernetUdp.h>
#include <LiquidCrystal.h>
//#include <stdlib.h>
// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>
#define LED_red 6 // the pin for the red LED
#define LED_grn 7 // the pin for the green LED
#define BUTTON 8 // input pin of the pushbutton
int btn_val = 0; // stores the state of the input pin
int shop_status = 0; // 0 = We're currently CLOSED
// 1 = We're currently OPEN
int status = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
// byte ip[] = { 192, 168, 2, 250 };
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
// IPAddress timeServer(0, 0, 0, 0); // Test case for no timestamp
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("etc-etc-etc");
// Messages to post
char msgOpen[] = "Xerocraft is open";
char msgClosed[] = "Xerocraft is closed";
char tweet[141]; // The tweet to send
char timestamp[8];
// ************************************************************
void setup()
{
pinMode(LED_red, OUTPUT); // tell Arduino LED_red is an output
pinMode(LED_grn, OUTPUT); // tell Arduino LED_grn is an output
pinMode(BUTTON, INPUT); // tell Arduino BUTTON is an input
// Serial.begin(9600);
lcd.begin(20, 4);
lcd.print("Activated."); << Appears on LCD display.
delay(2000);
lcd.clear();
lcd.print("Checking for internet connection."); << Also appears on display.
delay(2000);
lcd.clear();
// Ethernet.begin(mac);
if (Ethernet.begin(mac) == 0) { << Print statements to LCD stop working at this point, though the rest of the code continues to function properly.
// Serial.println("Failed to configure Ethernet using DHCP.");
// no point in carrying on, so do nothing forevermore:
for(;;) { // Change this
// Serial.println("Press RESET and try again.");
delay(10000);
}
}
lcd.print("UDP Start.");
delay(2000);
lcd.clear();
Udp.begin(localPort);
lcd.print("Ready.");
delay(2000);
lcd.clear();
// Serial.println("Ready.");
}
// ************************************************************
void loop()
{
// lcd.clear();
btn_val = digitalRead(BUTTON); // read input value and store it fresh
// check if button has been pressed
if ((btn_val == HIGH)) {
lcd.print("Button has been pressed!");
delay(2000);
lcd.clear();
// Serial.println("Button has been pressed!");
if (shop_status == 0) { // we were closed but now we're OPENING
// Serial.println("We're closed. Time to OPEN!");
// Serial.println("");
strcpy(tweet, msgOpen);
} else { // else shop_status == 1, which means we were open but now we're CLOSING
// Serial.println("We're open. Time to CLOSE!");
// Serial.println("");
strcpy(tweet, msgClosed);
}
if (getTime()) { // == 1, then the timestamp was created successfully
strcat(tweet, " as of ");
strcat(tweet, timestamp);
} else { // ELSE returned 0, send tweet w/o timestamp
// Serial.println("Sending tweet w/o timestamp."); // *WARNING*: Duplicate tweets will be rejected by Twitter!
}
strcat(tweet, "!");
if(sendTweet(tweet)) { // == 1 then tweet sent successfully
shop_status = 1 - shop_status;
lcd.print("Tweet sent: ");
lcd.print(tweet);
// Serial.print("Tweet sent: ");
// Serial.println(tweet);
// Serial.println("");
if (shop_status) { // == 1, then we're now OPEN
digitalWrite(LED_grn, HIGH); // Turn green LED on
digitalWrite(LED_red, LOW); // Turn red LED off
} else { // == 0, then we're now CLOSED
digitalWrite(LED_grn, LOW); // Turn green LED off
digitalWrite(LED_red, HIGH); // Turn red LED on
}
} else {
lcd.print("FAILURE! Tweet not sent. Try again in 1 min.");
// Serial.println("FAILURE! Tweet not sent. Try again in 1 min.");
// Flash red LED, return to previous LED state
digitalWrite(LED_red, LOW); // Turn red LED off
digitalWrite(LED_grn, LOW); // Turn green LED off
for (int x = 0; x < 7; x++) {
digitalWrite(LED_red, HIGH);
delay(300);
digitalWrite(LED_red, LOW);
delay(300);
}
if (shop_status) { // == 1, then we're now OPEN
digitalWrite(LED_grn, HIGH); // Turn green LED on
digitalWrite(LED_red, LOW); // Turn red LED off
} else { // == 0, then we're now CLOSED
digitalWrite(LED_grn, LOW); // Turn green LED off
digitalWrite(LED_red, HIGH); // Turn red LED on
}
}
code continues...