Hi, I thought I had my project all working but for some reason it is still not 100%.
Basically I have an AppleScript that is sending serial data to the Arduino Uno and the Uno is displaying it on an LCD.
It works great except for the fact that it only works when the Arduino serial monitor is open. At first it was the common problem of the board resetting with each serial read. I fixed this issue by cutting the trace on my Uno.
Currently, if I have my Uno running and the AppleScript sending data every 10 seconds, I will eventually get some corrupt data on my LCD.
As soon as I open the serial monitor and send data from my AppleScript, it works perfectly.
It's driving me nuts.
Any ideas?
Thanks.
Phil
#define SOP '<'
#define EOP '>'
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
bool started = false;
bool ended = false;
int countStrt = 0;
char inData[80];
byte index;
int timeout=0;
int timeoutFlag=0;
long previousCount = 0 ;
void setup()
{
Serial.begin(57600);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop()
{
if (timeout == 0 || timeout > 15) { // test to see if the Arduino is still waiting
previousCount = 0 ; // reset count
if (timeoutFlag==0) { // to avoid LCD refresh
lcd.clear();
lcd.print("Waiting for App....");
timeoutFlag =1;
}
}
timeout++;
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP) //wait for start packet
{
timeoutFlag=0; // reset timeout flag
timeout = 1; // got some data
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 20)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Serial.println(inData);
char* viewCount = inData; // convert inData to a string
long newCount = atol(viewCount);
// Serial.println(newCount);
if (newCount != previousCount ){
lcd.clear();
lcd.print("YouTube Views");
if (previousCount > 0){
for (long z=previousCount; z < newCount; z++){ //count up the counter
String newViewCount = String(z);
lcd.setCursor(0, 1);
lcd.print(newViewCount); // display viewCount
// Serial.println(newViewCount); // display viewCount
delay (10);
}
} // end count up loop
String newViewCount = String(newCount);
lcd.setCursor(0, 1);
lcd.print(viewCount); // display viewCount
// Serial.println(newViewCount); // display viewCount
// Reset for the next packet
previousCount = newCount;
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
delay (1000);
}