Switching on a light for one second every new tweets

Hello there! (good evening)

Context
My team and I are currently working on an Arduino project where the goal is to switch on an LED for one second each time a new tweet has been posted on our Twitter account.

Design
In order to do so, we used a Processing sketch. It writes on the Serial of the Arduino port every minute the last tweet posted on our account, if the tweet in question is different from the previously written one.

The Arduino sketch is supposed to monitor its Serial and gets the last message written by Processing, turning on its led each time the read tweet is different from the previous one, turning it off instead. The Arduino device used is an Arduino uno.

Problem
For some unknown reason, the LED only flickers when a new tweet is sent, and for a period of time less than one second. It should instead go on for one second then go off.

Here is the video showing the problem. We first uploaded the Arduino sketch to the Arduino uno, then launched the Processing sketch. The Processing sketch then gets the last tweet posted on our twitter account and write it on the Serial. As a result, the LED goes on. But it flickers for less than one second, where it should go on for one second then go off.

We do think the problem comes from Arduino and not Processing. Indeed, the Processing sketch is able to display to the Processing console the last tweet every minute, if it's different from the previous one.

Arduino code

/**
 * This sketch has for goal to make a certain LED of the Arduino board go on when a new tweet is sent from a certain Twitter account.
 * The only for the sketch to monitor new tweets is to listen to the Serial Monitor which a Processing sketch is writing to each time a new tweet is detected.
 */


String lastTweet;
String tweet;
int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  
  // getting the last String sent via the Serial Monitor and assigning this String to the tweet object
  if(Serial.available()) 
  {
    tweet = Serial.read();
  }
  
  // determining if the received tweet is different from the previous one and if so, turn on the LED
  if(tweet!=lastTweet)
  {
    lastTweet = tweet;
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  
  //wait for a certain time before running again
  delay(1000);
}

Cheers!

Why would the Arduino give a rats ass what that current tweet is, or what the last one was? The Processing app should take care of determining if there was a new tweet.

  // getting the last String sent via the Serial Monitor and assigning this String to the tweet object
  if(Serial.available())
  {
    tweet = Serial.read();
  }

No, that is NOT what that code is doing. RTFM for the Serial.read() method.

  //wait for a certain time before running again
  delay(1000);

Why? There is absolutely no reason to not check the serial buffer again immediately.

Good morning. The "tweet object" is a single character, right?

aarg:
Good morning. The “tweet object” is a single character, right?

No, it is a String. I think it is possible to send and receive String using the Serial isn’t it? I already did the same from Arduino to Processing.

PaulS:
Why would the Arduino give a rats ass what that current tweet is, or what the last one was? The Processing app should take care of determining if there was a new tweet.

Did you read my post? The Arduino doesn’t give a rats ass about about new tweets, the Processing sketch does. It sends messages to the Serial only when a new tweet has been detected. And the only way I found for Arduino to be aware of detection of a new tweet by the Processing sketch is to listen to the Serial and check if the received.

PaulS:
No, that is NOT what that code is doing. RTFM for the Serial.read() method.

RTFM, that’s ****ing helpful man, of course I didn’t check it because it’s easier to make and properly present a post on the forum and to upload a ****ing video with it.
Oh, did I mention I was joking. I read the manual.

  //wait for a certain time before running again
  delay(1000);

Why? There is absolutely no reason to not check the serial buffer again immediately.
[/quote]
Like it’s useless to check every millisecond if a new tweet has been sent when Processing checks for new tweets every minute? Furthermore, for each iteration of the loop() function, the LED is given a state, on or off. So, if I remove the delay, it would be supposed to just go on for a very short time.

I know I am wrong somewhere, and I’m trying to figure out what that is using notably the manual. Now if the only thing you can say is to tell me in a rude way to read the manual, I think I may not need your help for this particular problem.

moderator edit: LANGUAGE

Did you read my post? The Arduino doesn't give a rats ass about about new tweets, the Processing sketch does. It sends messages to the Serial only when a new tweet has been detected.

Why does it send the tweet, then? If you KNOW, on the Arduino side, that Processing is only sending data when there is a new tweet, why are you trying to compare the current tweet to the previous tweet?

The fact that there is serial data AT ALL is indicative of a new tweet (or should be).

The readString() or readStringUntil() methods will read a String. The read() method does NOT.

of course I didn't check it because it's easier to make and properly present a post on the forum

that illustrated that you didn't RTFM. Good job.

I think I may not need your help for this particular problem.

You just posted to say that you have a problem, and to wish us a nice day? I must have missed that part. My sincere apologies.

Well, first of all it looks like you don't understand caps don't help me understand what you are trying to say.

PaulS:
that illustrated that you didn't RTFM. Good job.

May be you read my post in the reverse order, so here is what I said just after this:

Oh, did I mention I was joking. I read the manual.

PaulS:
You just posted to say that you have a problem, and to wish us a nice day? I must have missed that part. My sincere apologies.

Don't worry, you didn't understand what I said. I was only talking about you. I need help, not some pretentious rude comments. Your apologies would be sincere if you weren't being rude in the same post by the way.

Your apologies would be sincere if you weren't being rude in the same post by the way.

I wasn't even trying to be sincere.

PaulS:
I wasn't even trying to be sincere.

I actually quite understood that. The fact is, you're complaining about me refusing help, when I just say that I don't consider "RFTM" as any kind of help.

The fact is, you're complaining about me refusing help, when I just say that I don't consider "RFTM" as any kind of help.

If you had read the manual, you'd know what read() does, and you'd know that storing one character in a String is silly.

I asked other questions, too, and you got defensive, insisting that you knew what you were doing, implying that the fact that your code doesn't work is somehow our fault.

If you actually read all the comments and questions, AND provide answers (and look at the methods that have been suggested), you just might get somewhere.

Or, you can continue to not accept responsibility for your code. Your choice.

PaulS:
If you had read the manual, you'd know what read() does, and you'd know that storing one character in a String is silly.

I have not recorded myself reading the manual, yet I read the read() documentation and didn't understand it wasn't about String.

PaulS:
If you actually read all the comments and questions, AND provide answers (and look at the methods that have been suggested), you just might get somewhere.

Talking about reading everything:

Good morning. The "tweet object" is a single character, right?

No, it is a String. I think it is possible to send and receive String using the Serial isn't it? I already did the same from Arduino to Processing.

Why? There is absolutely no reason to not check the serial buffer again immediately.

Like it's useless to check every millisecond if a new tweet has been sent when Processing checks for new tweets every minute? Furthermore, for each iteration of the loop() function, the LED is given a state, on or off. So, if I remove the delay, it would be supposed to just go on for a very short time.

I wasn't defensive of my code on the last quote

PaulS:
I asked other questions, too, and you got defensive, insisting that you knew what you were doing, implying that the fact that your code doesn't work is somehow our fault.

I know I am wrong somewhere, and I'm trying to figure out what that is using notably the manual.

I'm not defensive, I sound rude because I got a reply with caps, "ass rats" and "RTFM" implying that's everything I did was obviously wrong. I will try what you told me. I don't mind being wrong, as long as I can be told what's wrong not in a pretentious and disdainful way, otherwise I'll just reply the same way as it sounds fair to me. Are we done here?

I read the read() documentation and didn't understand it wasn't about String.

From here

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

Mind your language please, ladies.

Are we done here?

If your code is working the way you want, yes. If not, no.

Design
In order to do so, we used a Processing sketch. It writes on the Serial of the Arduino port every minute the last tweet posted on our account, if the tweet in question is different from the previously written one.

When you say it writes the tweet to the serial, how does it indicate that it has finished writing the tweet? Does the tweet finish off with a delimiter? Is there some way, by looking at the bytes coming in on the serial, that we can work out that the tweet has finished coming? Or is it all timing based?

In any case, your sketch reads the incoming bytes one letter at a time and puts them into a one-character-long string. Then it waits for a second, during which time the serial buffer fills up with stuff and probably overflows. Then it looks at the next incoming letter, and compares it to the last letter it read.

I would expect this sketch to leave the LED on for several seconds while it went through the letters in the tweet one letter per second, except occasionally turning the led off for a second when the tweet happened to contain a repeated character.

const unsigned long LED_ON_ms = 1000;
const unsigned long PAUSE_ms = 1000 * 3; // three seconds. Meh

boolean pausing_between_tweets;
unsigned long start_of_tweet_ms;
String tweet;

void setup() {
  pausing_between_tweets = true;
  start_of_tweet_ms = millis() - PAUSE_ms;
}

void loop() {
  if(serial.available()) {
    // we have some stuff

    if(pausing_between_tweets) {
      // we have just started recieving a tweet!
      start_of_tweet_ms = millis();
      pausing_between_tweets = false;
    }

    while(serial.available()) {
      tweet += serial.read(); // add the serial data to the current tweet
    }

  }
  else {
    // no incoming
    if(!pausing_between_tweets && (millis() - start_of_tweet_ms) > PAUSE_ms ) {
      // there has been nothing for PAUSE seconds. The tweet is now fully read. 
      output_the_tweet_to_the_LCD(); // or whatever. 
      tweet = "";
      pausing_between_tweets = true;
    }
  }

  // LED is on for one second from the START of receiving a tweet.
  digitalWrite(millis() - start_of_tweet_ms > LED_ON_ms ? LOW : HIGH);
   
}

I'm not defensive, I sound rude because I got a reply with caps, "ass rats" and "RTFM" implying that's everything I did was obviously wrong. I will try what you told me. I don't mind being wrong, as long as I can be told what's wrong not in a pretentious and disdainful way, otherwise I'll just reply the same way as it sounds fair to me. Are we done here?

Welcome to the forum. I see you have met the forum troll. Below is some simple serial LED control code that could be used on the arduino with the processing program doing most of the required logic and timing. If you have the tweet capturing worked out on the pc, then the rest is probably easy.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }

    readString="";
  } 
}

CCCXLIII:
Furthermore, for each iteration of the loop() function, the LED is given a state, on or off. So, if I remove the delay, it would be supposed to just go on for a very short time.

Not if you embrace the thinking of BlinkWithOutDelay (affectionally known as BWOD), also found in the IDE File > Examples > 2.Digital. This video shows it nicely. Whole point is that each time through loop(), you look at the clock to see if it's time to turn the led off or on. So it could blink on for a day and off for a week if you like, without holding up the rest of the code.