Talking To The Cloud

i'm new to Arduino and I am struggling making the code from Example 08B 'Getting Started with Arduino' work. The processing element of the project works fine (I can get the word counter to display on screen) but when uploading the Arduino code it does not seem to have any effect on the LED circuit. As suggested I have tried putting the serial port number in the processing code but when trying to run this an error occurs (ArrayIndexOutOfBoundsException: 4).
I have checked the circuit and all seems well.

I'm sure i'm missing something very simple !!!

Even though it is an example from a book, you might have fat-fingered something typing it in. Post the code you are actually running, both the Arduino code and the Processing code. You are much more likely to get help that way.

An ArrayIndexOutOfBounds exception would not be caused by a bad port number/name.

It could be caused by trying to reference the 2nd entry in a list of 1 port names, though.

Thanks for the reply, I have posted the code below: The processing code works as a stand alone program and does display the counter on screen. Arduino doesn't respond when uploading it's own code though.

The notes are from the book (not my own) and was pasted over from www.makezine.com

Proceesing code first - Arduino code follows:

Processing:

// Example 08A: Arduino networked lamp
// parts of the code are inspired
// by a blog post by Tod E. Kurt (todbot.com)
//
// Copy and paste this example into an empty Processing sketch

import processing.serial.*;

String feed = "http://blog.makezine.com/index.xml";

int interval = 10;  // retrieve feed every 60 seconds;
int lastTime;       // the last time we fetched the content

int love    = 0;
int peace   = 0;
int arduino = 0;

int light = 0;  // light level measured by the lamp

Serial port;
color c;
String cs;

String buffer = ""; // Accumulates characters coming from Arduino

PFont font;

void setup() {
 size(640,480);
 frameRate(10);    // we don't need fast updates

 font = loadFont("HelveticaNeue-Bold-32.vlw");  
 fill(255);  
 textFont(font, 32);
 // IMPORTANT NOTE:
 // The first serial port retrieved by Serial.list()
 // should be your Arduino. If not, uncomment the next
 // line by deleting the // before it, and re-run the
 // sketch to see a list of serial ports. Then, change
 // the 0 in between [ and ] to the number of the port
 // that your Arduino is connected to.
 //println(Serial.list());
 String arduinoPort = Serial.list()[0];
 port = new Serial(this, arduinoPort, 9600); // connect to Arduino

 lastTime = 0;
 fetchData();
}

void draw() {
 background( c );
 int n = (interval - ((millis()-lastTime)/1000));

 // Build a colour based on the 3 values
 c = color(peace, love, arduino);
 cs = "#" + hex(c,6); // Prepare a string to be sent to Arduino

 text("Arduino Networked Lamp", 10,40);
 text("Reading feed:", 10, 100);
 text(feed, 10, 140);

 text("Next update in "+ n + " seconds",10,450);
 text("peace" ,10,200);
 text(" " + peace, 130, 200);
 rect(200,172, peace, 28);

 text("love ",10,240);
 text(" " + love, 130, 240);
 rect(200,212, love, 28);

 text("arduino ",10,280);
 text(" " + arduino, 130, 280);
 rect(200,252, arduino, 28);

 // write the colour string to the screen
 text("sending", 10, 340);
 text(cs, 200,340);

 text("light level", 10, 380);
 rect(200, 352,light/10.23,28); // this turns 1023 into 100

 if (n <= 0) {
   fetchData();
   lastTime = millis();
 }

 port.write(cs); // send data to Arduino

 if (port.available() > 0) { // check if there is data waiting
   int inByte = port.read(); // read one byte
   if (inByte != 10) { // if byte is not newline
     buffer = buffer + char(inByte); // just add it to the buffer
   }
   else {

     // newline reached, let's process the data
     if (buffer.length() > 1) { // make sure there is enough data

       // chop off the last character, it's a carriage return
       // (a carriage return is the character at the end of a
       // line of text)
       buffer = buffer.substring(0,buffer.length() -1);

       // turn the buffer from string into an integer number
       light = int(buffer);

       // clean the buffer for the next read cycle
       buffer = "";

       // We're likely falling behind in taking readings
       // from Arduino. So let's clear the backlog of
       // incoming sensor readings so the next reading is
       // up-to-date.
       port.clear(); 
     }
   } 
 }

}

void fetchData() {
 // we use these strings to parse the feed
 String data; 
 String chunk;

 // zero the counters
 love    = 0;
 peace   = 0;
 arduino = 0;
 try {
   URL url = new URL(feed);  // An object to represent the URL
   // prepare a connection   
   URLConnection conn = url.openConnection(); 
   conn.connect(); // now connect to the Website

   // this is a bit of virtual plumbing as we connect
   // the data coming from the connection to a buffered
   // reader that reads the data one line at a time.
   BufferedReader in = new
     BufferedReader(new InputStreamReader(conn.getInputStream()));

   // read each line from the feed
   while ((data = in.readLine()) != null) {

     StringTokenizer st =
       new StringTokenizer(data,"\"<>,.()[] ");// break it down
     while (st.hasMoreTokens()) {
       // each chunk of data is made lowercase
       chunk= st.nextToken().toLowerCase() ;

       if (chunk.indexOf("love") >= 0 ) // found "love"?
         love++;    // increment love by 1
       if (chunk.indexOf("peace") >= 0)   // found "peace"?
         peace++;   // increment peace by 1
       if (chunk.indexOf("arduino") >= 0) // found "arduino"?
         arduino++; // increment arduino by 1
     }
   }

   // Set 64 to be the maximum number of references we care about.
   if (peace > 64)   peace = 64;
   if (love > 64)    love = 64;
   if (arduino > 64) arduino = 64;

   peace = peace * 4;     // multiply by 4 so that the max is 255,
   love = love * 4;       // which comes in handy when building a
   arduino = arduino * 4; // colour that is made of 4 bytes (ARGB)
 } 
 catch (Exception ex) { // If there was an error, stop the sketch
   ex.printStackTrace();
   System.out.println("ERROR: "+ex.getMessage());
 }

}

Arduino Code:

// Example 08B: Arduino Networked Lamp
//
// Copy and paste this example into an empty Arduino sketch

#define SENSOR 0   
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12

int val = 0; // variable to store the value coming from the sensor

int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;

byte r = 0;
byte g = 0;
byte b = 0;

void setup() {
 Serial.begin(9600);  // open the serial port
 pinMode(BUTTON, INPUT);
}

void loop() {
 val = analogRead(SENSOR); // read the value from the sensor
 Serial.println(val);      // print the value to
                           // the serial port

 if (Serial.available() >0) {

   // read the incoming byte:
   inByte = Serial.read();

   // If the marker's found, next 6 characters are the colour
   if (inByte == '#') {

     while (pointer < 6) { // accumulate 6 chars
       buffer[pointer] = Serial.read(); // store in the buffer
       pointer++; // move the pointer forward by 1
     }

     // now we have the 3 numbers stored as hex numbers
     // we need to decode them into 3 bytes r, g and b
     r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
     g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
     b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
     pointer = 0; // reset the pointer so we can reuse the buffer
    
   }
 }   

 btn = digitalRead(BUTTON); // read input value and store it

 // Check if there was a transition
 if ((btn == HIGH) && (old_btn == LOW)){
   state = 1 - state;
 }

 old_btn = btn; // val is now old, let's store it

 if (state == 1) { // if the lamp is on

   analogWrite(R_LED, r);  // turn the leds on
   analogWrite(G_LED, g);  // at the colour
   analogWrite(B_LED, b);  // sent by the computer
 } else {

   analogWrite(R_LED, 0);  // otherwise turn off
   analogWrite(G_LED, 0); 
   analogWrite(B_LED, 0);
  }

 delay(100);                // wait 100ms between each send
}

int hex2dec(byte c) { // converts one HEX character into a number
   if (c >= '0' && c <= '9') {
     return c - '0';
   } else if (c >= 'A' && c <= 'F') {
     return c - 'A' + 10;
   }
}

Thanks for your help

Ow! My poor scrolling finger.
Can you go back to your last post, click on "modify", then highlight all the code, and click on the "#" button on the toolbar, then click on the "save" button.

Thanks for the response and apologies for busting your finger with all the scrolling.

I'm sure I did connect the grounds but I will re-try (it wouldn't suprise me if I didn't)

Thanks again.

Arduino doesn't respond when uploading it's own code though.

I don't understand what you are trying to say, here. Are you having trouble uploading code to the Arduino?

Reply to PaulS:
Thanks for the response.
The code does upload to Arduino but the desired effect - to have the LED's respond with varying colours to the word count for 'peace', 'love' & 'Arduino' by relating to the data gathered by the processing portion of the code - does not seem to be happening.
When uploading the Arduino code the LED's do flicker so there is life there somewhere, but just not as we know it !!
I'm sure I am making some fundamental error and more than likely (as mentioned by AWOL) it is circuit related but I have checked and checked again and can't see anything obvious. I intend to start again from scratch tonight and see how that helps.

All the best

Create a simple sketch that lights up the LEDs in order, for 1 second. Use that to test the hardware. When the hardware works, load the sketch that reads the serial port to get the data to light the LEDs.

Reply to PaulS:

Thanks for that, sounds like a logical solution, will give it a go and report back.

All the best

Reply to PaulS:

I have put together a simple code for the LED's as you suggested and the code does seem to work:

#define LED 13
#define LED 12
#define LED 11

void setup() {
  pinMode (LED, OUTPUT);
  
  int state = 0;
  
}

void loop() {
  {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(12, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    delay(1000);
    digitalWrite(11, HIGH);
    delay(1000);
    digitalWrite(11, LOW);
    delay(1000);
    }
  
}

However, I don't know if my circuit building skills are letting me down again (which makes it even more likely that the original problem posted could be circuit related). The LED's light up in the correct sequence with the correct delay but they are different intensities, LED13 quite bright, LED12 very dim, LED11 the brightest. I have used all the same resistor types for each LED. Also the Arduino LED lights when LED13 is on (I don't know if this is normal).

Any help would be appreciated.

All the best.

This:

#define LED 13
#define LED 12
#define LED 11

simply redefines the value of LED. It does not create 3 instances of anything.

This:

  pinMode (LED, OUTPUT);

sets pin 11 (the last value for LED) to an output pin. Pin 12's mode id not defined, so it defaults to input.

Pin 13 has an on-board LED connected to it, so the on-board LED lighting up, when pin 13 is set HIGH, is proper.

The fact that pin 12 is dim is because it has not been defined as an output PIN.

Thanks for that, I have re-jigged the code and all works fine now, it's so handy to have you guys for help , it would have taken quite some time to muddle through a solution myself.

Now on to getting to grips with the original issue !!

I'm sure you will hear from me again.

Cheers

After my success in getting the LED's to light with one second intervals (thanks to your advice), i tried the original code again for both processing and Arduino but it still does not work. Apparently (with the Arduino code) you are supposed to get a register on-screen of the sensor value but this do not happen and the LED's do nothing, and following the 'IMPORTANT NOTE' in the processing code doesn't seem to work either.

I have checked the circuit again and again but all seems ok and the code is simply pasted from the makezine website so I would expect no errors.

Through this site I have found a far better introductory manual (ASK manual) than the 'Getting Started with Arduino' book I am using so I may give this one up as a bad job and work my way through the projects which seem to be explained in far more detail.

I do hate leaving things un-resolved though so If you can think of anything last thing I can try I would be grateful.

All the best

If you un-comment the println statement in the Processing sketch, what gets printed at the bottom of the Processing window?

Which serial port are you using, in the Arduino IDE to talk to the Arduino (to upload sketches to)?

Hi, PaulS, I get the following message:

[0] "COM3"
[1] "COM4"

I have checked and the Arduino port is COM4

String arduinoPort = Serial.list()[[glow]0[/glow]];

So, you want to be using a 1 here, not 0.

Thanks for that, it is all starting to make sense now... :slight_smile:

This is getting on my wick now !!!!

I have put the [1] code in but now I am getting the following mssg when uploading the Arduino Code:

Serial Port "COM 4" already in use. Try quitting any programs that may be using it

I also get the following code in the window:

processing.app.SerialException: Serial port 'COM4' already in use.  Try quiting any programs that may be using it.
      at processing.app.Serial.<init>(Serial.java:144)
      at processing.app.Serial.<init>(Serial.java:76)
      at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:73)
      at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:83)
      at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:55)
      at processing.app.Sketch.upload(Sketch.java:1498)
      at processing.app.Sketch.exportApplet(Sketch.java:1465)
      at processing.app.Sketch.exportApplet(Sketch.java:1420)
      at processing.app.Editor$46.run(Editor.java:2219)
      at java.lang.Thread.run(Thread.java:619)
processing.app.debug.RunnerException: Serial port 'COM4' already in use.  Try quiting any programs that may be using it.
      at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:93)
      at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:83)
      at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:55)
      at processing.app.Sketch.upload(Sketch.java:1498)
      at processing.app.Sketch.exportApplet(Sketch.java:1465)
      at processing.app.Sketch.exportApplet(Sketch.java:1420)
      at processing.app.Editor$46.run(Editor.java:2219)
      at java.lang.Thread.run(Thread.java:619)
processing.app.debug.RunnerException: Serial port 'COM4' already in use.  Try quiting any programs that may be using it.
      at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:93)
      at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:83)
      at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:55)
      at processing.app.Sketch.upload(Sketch.java:1498)
      at processing.app.Sketch.exportApplet(Sketch.java:1465)
      at processing.app.Sketch.exportApplet(Sketch.java:1420)
      at processing.app.Editor$46.run(Editor.java:2219)
      at java.lang.Thread.run(Thread.java:619)

I assume "COM4" is in use because the Arduino is using it ?!!!

Do you have the Serial Monitor window open, too? You can't. If Processing is to use the port, the Serial Monitor can't.

I did have the Serial Monitor Window open and when i closed it for the Arduino upload I didn't get the "COM4" error mssg, which is good.

However, nothing happens with the LED's still, it's weird.

I did notice that on the Serial Monitor Window the LDR is registering the amount of light exposure, so that would suggest that there is some communication going on but the Button and LED's seem redundant.

I did go back and check the LED's with the blinking code you suggested and noticed that the circuit in the book has the GND and PIN jump leads the opposite way around to how I have to have them to get the LED's to blink. On the breadboard I have the PIN jumpers on the positive of the LED and the GNDs on the resistor side and this works for the blinking LEDs (am I maybe going wrong here).

Sorry for having to keep coming back with more questions !! I just can't let it get the better of me.