Twitter controled pumpkin - using Temboo and Arduino Yun

Hi All,

I'm really new to Arduino and don't have to much coding experience. I've decided to give a try and build a Twitter controlled pumpkin using Temboo and Arduino Yun.

The code seems to be working except for one thing. The yellow LED that I wanted to use as a main light source for the pumpkin (in candle mode) is not working as planned. Would you be able to have a look on my code and help me figure this out please?

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below


#define CANDLELED 11
#define REDLED 2
#define BUZZER 7
#define BUZZER_FREQUENCY 38
#define FLICKER_INTERVAL 25

long previousMillis = 0;

String last_id;

unsigned char candeled_state;



int numRuns = 1;   // Execution count, so this doesn't run forever
int maxRuns = 250;   // Maximum number of times the Choreo should be executed

void setup() {
  pinMode(CANDLELED, OUTPUT);
  pinMode(REDLED, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  Bridge.begin();
 // Console.begin();
 // while(!Console);
  delay(1000);  
}

void loop() {
  {
  if(candeled_state == 0) {
     digitalWrite(CANDLELED, LOW);
				digitalWrite(REDLED, HIGH);
				tone(BUZZER, BUZZER_FREQUENCY);
                                delay(5000);
                                noTone(BUZZER);
		                digitalWrite(REDLED, LOW);
                                unsigned long currentMillis = millis();
                		
                
                if (currentMillis - previousMillis > FLICKER_INTERVAL)
		{
                        for(int i = 0; i < 100; i++)
                        previousMillis = currentMillis;
			analogWrite(CANDLELED, random(0, 256));
                        
                 }
			
  }
      
else {
                               
                noTone(BUZZER);
		digitalWrite(REDLED, LOW);
                unsigned long currentMillis = millis();
		if (currentMillis - previousMillis > FLICKER_INTERVAL)
		{
                        
                        previousMillis = currentMillis;
			analogWrite(CANDLELED, random(0, 256));
		}

}
  }
 
  {
  if (numRuns <= maxRuns) {
    // Console.println("Running Tweets - Run #" + String(numRuns++));
    
    TembooChoreo TweetsChoreo;
   
    // Invoke the Temboo client
    TweetsChoreo.begin();
    
    // Set Temboo account credentials
    TweetsChoreo.setAccountName(TEMBOO_ACCOUNT);
    TweetsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    TweetsChoreo.setAppKey(TEMBOO_APP_KEY);
    
    // Set profile to use for execution
    TweetsChoreo.setProfile("search");
    
    // Identify the Choreo to run
    TweetsChoreo.setChoreo("/Library/Twitter/Search/Tweets");
    
   TweetsChoreo.addOutputFilter("id", "/[1]/id_str", "Response");
    
     // tell the Process to run and wait for the results. The 
    // return code will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = TweetsChoreo.run();

     
    // a response code of 0 means success; print the API response
    if(returnCode == 0) {

      // String author; // a String to hold the tweet author's name
      // String tweet; // a String to hold the text of the tweet
     String id;

        // choreo outputs are returned as key/value pairs, delimited with 
      // newlines and record/field terminator characters, for example:
      // Name1\n\x1F
      // Value1\n\x1E
      // Name2\n\x1F
      // Value2\n\x1E  
  
      // see the examples at http://www.temboo.com/arduino for more details
      // we can read this format into separate variables, as follows:
      while(TweetsChoreo.available()) {
 
        String name = TweetsChoreo.readStringUntil('\x1F');
        name.trim();

        // read the value of the output item
        String data = TweetsChoreo.readStringUntil('\x1E');
        data.trim();

        // assign the value to the appropriate String
        if (name == "id") {
          id = data;
        } 

      }
   { 
    if((last_id != id) ) {
       
                                candeled_state = 0;
                                delay(5000);
                                candeled_state = 1;
     
    } 
    else {
     
       candeled_state = 1;
    }
      
    
    TweetsChoreo.close();
    
  }

  Console.println("Waiting...");
  delay(60000); // wait 60 seconds between Tweets calls
  
}
   }
  }
  }

I've built this using the code from here:
https://raw.githubusercontent.com/mrichardson23/PimpYourPumpkin/master/PimpYourPumpkin.pde

and here:

Thanks so much for all your help.

Martin

Basically the problem is that the LED is not flickering, only changes the brightness when the loop() function restarts. The "for" loop seems to be fixing this but it's hard to find for how long it should run.

The problem is that you have many delays in your loop, including a very long one at the end. Nothing else happens during a delay, but in your case you still want to keep updating the LED so that it flickers. It's going to take some work to fix this.

The typical solution is to use the millis() function to schedule the different operations: you can get more information by reading up on "blink without delay." But that's going to be difficult in this situation since you have so many sequential operations in your loop, it can get messy fast. Also, it will take some time while your Temboo call completes, so you will still have some pauses in your flicker.

While it's a much more advanced topic, you should read up on "timer interrupts." The idea is that you set up the hardware to periodically generate interrupts, which will call a function at regular time intervals. Inside that function you put the few lines of code that actually update the LED to make it flicker, and leave everything else in loop(). That way, the code in loop() can take all the time it wants, and the LED will keep on flickering. You are using the tone() and analogwrite() functions: both of these already use timer hardware, so you will have to take some care that setting up the timer doesn't interfere with those functions. It will get tricky - but you will learn a LOT by the time you get it working. The goal is that this is the only part that should be in the interrupt handler:

                unsigned long currentMillis = millis();
		if (currentMillis - previousMillis > FLICKER_INTERVAL)
		{
                        
                        previousMillis = currentMillis;
			analogWrite(CANDLELED, random(0, 256));
		}

Note that this section of code already uses the "blink without delay" concepts. The issue is that there are still delays in your loop: you either have to get rid of ALL delay calls, or go with timer interrupts.

Thanks ShapeShifter. I have change the sketch to just turn on the CandleLED for now instead of flickering. I'll have a look into timer interrupts but wanted to have this running ASAP before Halloween :slight_smile:

Although it looks like the sketch doesn't work as planned but I think it's more related to Twitter Search API. Even when I'm running test run directly in Temboo (through a web browser) the test tweets with the hashtag that I wanted to use for this are not showing on Twitter Search API.

rambo8wtv:
Even when I'm running test run directly in Temboo (through a web browser) the test tweets with the hashtag that I wanted to use for this are not showing on Twitter Search API.

Yes, it sounds like you have a couple issues here. I agree that until you can get the search API working properly using the Temboo web pages, there isn't much point in working it from the sketch. Don't try to do too much at once so you can eliminate some variables: first try to get the searches working on the Temboo web page. Once that works just the way you want, then get that same search working though your sketch. Once that works, put some time into getting the flicker working properly.

The key is taking baby steps: when you try to do too much at once, it's hard to figure out just where the trouble might be. But when taking small steps, if something is not working, it's probably related to that last step you were trying to take.

Good luck! And please report back how things are working out.