New to spark and new to coding (First Project)

Hi Everyone,

Wanted to start by saying I just got my core and its awesome I love it :smiley: that being said i’m new to all this code stuff I’ve been making things and playing with electronics since I was 10 when I got my first soldering iron lol. So I found a cool project I want to make (http://www.instructables.com/id/Twitter-Mood-Light-The-Worlds-Mood-in-a-Box/) and Ive sorted the hardware side of things in 10mins but I then see the code and Im just like WOOOW thats confusing :frowning: Can anybody point me in the right direction, So far this is where im at and im not even sure if this is right :wink:

int redpin = D0;
int bluepin = D1;
int greenpin = D2;

void setup() 
{
   pinMode(redpin, OUTPUT);
   pinMode(bluepin), OUTPUT);
   pinMode(greenpin), OUTPUT);

   digitalWrite(redpin, LOW);
   digitalWrite(bluepin, LOW);
   digitalWrite(greenpin, LOW);
}

void loop() 
{

}

@mumbles,

Please read the thread on Forum Tips to learn how to paste code in the forum with properly formatting. :wink:

What kind of help do you need?

int redpin = D0;
int bluepin = D1;
int greenpin = D2;

void setup() 
{
   pinMode(redpin, OUTPUT);
   pinMode(bluepin), OUTPUT);
   pinMode(greenpin), OUTPUT);

   digitalWrite(redpin, LOW);
   digitalWrite(bluepin, LOW);
   digitalWrite(greenpin, LOW);
}

void loop() 
{

}

So like that? and I just want to understand the code and starting to realize thats probably not going to be a five minute lesson, so is their like a tutorial I can read somewhere explaining the basics ?

Maybe here is a good place and there are a ton of resources online :wink:

http://arduino.cc/en/Tutorial/HomePage

So is the arduino code the same as spark code ? :smile:

The spark core coding environment is Arduino compatible but more feature-rich than Arduino as you tinker more.

Have fun!

So in theory I can use the code as is? with some slight modifications to the pin locations

// LED setup - only some pins provide 8-bit PWM (Pulse-width modulation)
// output with the analogWrite() function.
// http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
// PWM: 3,5,6,9,10,11
#define redPin    (3)
#define greenPin (5)
#define bluePin   (6)
 
// delay in ms between fade updates
// max fade time = 255 * 15 = 3.825s
#define fadeDelay (15)
 
// Wifi setup
#define network ([your network])
#define password ([your network password])
#define remoteServer ("twitter.com")
 
const char* moodNames[NUM_MOOD_TYPES] = {
 "love",
 "joy",
 "surprise",
 "anger",
 "envy",
 "sadness",
 "fear",
};
 
const char* moodIntensityNames[NUM_MOOD_INTENSITY] = {
 "mild",
 "considerable",
 "extreme",
};
 
// the long term ratios between tweets with emotional content
// as discovered by using the below search terms over a period of time.
float tempramentRatios[NUM_MOOD_TYPES] = {
 0.13f,
 0.15f,
 0.20f,
 0.14f,
 0.16f,
 0.12f,
 0.10f,
};
 
// these numbers can be tweaked to get the system to be more or less reactive
// to be more or less susceptible to noise or short term emotional blips, like sport results
// or bigger events, like world disasters
#define emotionSmoothingFactor (0.1f)
#define moodSmoothingFactor (0.05f)
#define moderateMoodThreshold (2.0f)
#define extremeMoodThreshold (4.0f)
 
// save battery, put the wifly to sleep for this long between searches (in ms)
#define SLEEP_TIME_BETWEEN_SEARCHES (1000 * 5)
 
// Store search strings in flash (program) memory instead of SRAM.
// http://www.arduino.cc/en/Reference/PROGMEM
// edit TWEETS_PER_PAGE if changing the rpp value
prog_char string_0[] PROGMEM = "GET /search.json?q=\"i+love+you\"+OR+\"i+love+her\"+OR+\"i+love+him\"+OR+\"all+my+love\"+OR+\"i'm+in+love\"+OR+\"i+really+love\"&rpp=30&result_type=recent";
prog_char string_1[] PROGMEM = "GET /search.json?q=\"happiest\"+OR+\"so+happy\"+OR+\"so+excited\"+OR+\"i'm+happy\"+OR+\"woot\"+OR+\"w00t\"&rpp=30&result_type=recent";
prog_char string_2[] PROGMEM = "GET /search.json?q=\"wow\"+OR+\"O_o\"+OR+\"can't+believe\"+OR+\"wtf\"+OR+\"unbelievable\"&rpp=30&result_type=recent";
prog_char string_3[] PROGMEM = "GET /search.json?q=\"i+hate\"+OR+\"really+angry\"+OR+\"i+am+mad\"+OR+\"really+hate\"+OR+\"so+angry\"&rpp=30&result_type=recent";
prog_char string_4[] PROGMEM = "GET /search.json?q=\"i+wish+i\"+OR+\"i'm+envious\"+OR+ \"i'm+jealous\"+OR+\"i+want+to+be\"+OR+\"why+can't+i\"+&rpp=30&result_type=recent";
prog_char string_5[] PROGMEM = "GET /search.json?q=\"i'm+so+sad\"+OR+\"i'm+heartbroken\"+OR+\"i'm+so+upset\"+OR+\"i'm+depressed\"+OR+\"i+can't+stop+crying\"&rpp=30&result_type=recent";
prog_char string_6[] PROGMEM = "GET /search.json?q=\"i'm+so+scared\"+OR+\"i'm+really+scared\"+OR+\"i'm+terrified\"+OR+\"i'm+really+afraid\"+OR+\"so+scared+i\"&rpp=30&result_type=recent";
 
// be sure to change this if you edit the rpp value above
#define TWEETS_PER_PAGE (30)
 
PROGMEM const char *searchStrings[] =        
{  
 string_0,
 string_1,
 string_2,
 string_3,
 string_4,
 string_5,
 string_6,
};
 
void setup()
{
 Serial.begin(9600);
 delay(100);
}
 
void loop()
{
 // create and initialise the subsystems 
 WiFly wifly(network, password, SLEEP_TIME_BETWEEN_SEARCHES, Serial);
 WorldMood worldMood(Serial, emotionSmoothingFactor, moodSmoothingFactor, moderateMoodThreshold, extremeMoodThreshold, tempramentRatios);
 LED led(Serial, redPin, greenPin, bluePin, fadeDelay);
 TwitterParser twitterSearchParser(Serial, TWEETS_PER_PAGE);
 
 wifly.Reset();
 
 char searchString[160];
 
 while (true)
 {
    for (int i = 0; i < NUM_MOOD_TYPES; i++)
    {
      twitterSearchParser.Reset();
 
      // read in new search string to SRAM from flash memory
      strcpy_P(searchString, (char*)pgm_read_word(&(searchStrings[i])));
 
      bool ok = false;
      int retries = 0;
 
      // some recovery code if the web request fails
      while (!ok)
      {
        ok = wifly.HttpWebRequest(remoteServer, searchString, &twitterSearchParser);
 
        if (!ok)
        {
          Serial.println("HttpWebRequest failed");
 
          retries++;
          if (retries > 3)
          {
            wifly.Reset();
            retries = 0;
          }
        }
      }
 
      float tweetsPerMinute = twitterSearchParser.GetTweetsPerMinute();
 
      // debug code
      Serial.println("");
      Serial.print(moodNames[i]);
      Serial.print(": tweets per min = ");
      Serial.println(tweetsPerMinute);
 
      worldMood.RegisterTweets(i, tweetsPerMinute);
    }
 
    MOOD_TYPE newMood = worldMood.ComputeCurrentMood();
    MOOD_INTENSITY newMoodIntensity = worldMood.ComputeCurrentMoodIntensity();
 
    Serial.print("The Mood of the World is ... ");
    Serial.print(moodIntensityNames[(int)newMoodIntensity]);
    Serial.print(" ");
    Serial.println(moodNames[(int)newMood]);
 
    led.SetColor((int)newMood, (int)newMoodIntensity);
 
    // save the battery
    wifly.Sleep();
 
    // wait until it is time for the next update
    delay(SLEEP_TIME_BETWEEN_SEARCHES);
 
    Serial.println("");
 }
}

Although the Arduino code and the Spark code share a lot of similarities, there are some subtle differences. This however should not be a problem if you’re just starting out. The most basic stuff is mostly the same, as is the logic behind the programming. The differences mostly come down to some alterations in function names, and more importantly, the additional features the Core offers. The Arduino doesn’t need a “publish” function, since it isn’t capable enough to actually do something with it, whereas the Core is. Many, if not all, of these functions are mentioned in the documentation, which can be found here: [docs.spark.io][1]
For beginner level stuff (“blink an LED”) you can follow Arduino tutorials (using the Core), which should mostly work without much effort. Once you progress through those, you’ll find that your knowledge increases, and you’ll be able to tackle more challenging things with the Spark.

Concerning the code in your first post:
If you’d like to be able to change colours fluently, you should consider using the analogWrite function, because it gives you 255 levels of control per colour, as opposed to on/off by using digitalWrite.

Concerning your latest code:
That will probably not work. Like I mentioned above: the code is similar, not identical. Also, it contains some “wifly” functions, which is rather hardware specific I believe.
[1]: http://docs.spark.io

1 Like

Thankyou for all your help guys, slowly but surely working through all the basic projects :slight_smile:

1 Like