Help with code in the book Make: Getting Started with Arduino

Hello,

I’m new and with no coding experience. I’m trying to learn Arduino with a book of Make, I purchased the book in 2020, yea procrastination sometimes is big. The thing is that when I try to write this code it gives me this error with red lines, even writing exactly as the book shows it.

I compared it with the same code I found in Github, and it is the same. I even copied and pasted in Processing IDE and it is even gives me more unexplained errors.

Please apologize my ignorance.

Please post the entire code between two sets of three graeve characters, ie

```
post code here.
```

or simpler yet click on the backward slash forward icon in the reply window and copy/paste your code into the gray area (set processing as the language in the drop down menu). We’re happy to help you, but we need to see the entire code (or at least more than your image shows). Also when you post the entire code we can try running it on our own systems.

Hello @JTR,

Welcome to the forum.

We all have to start somewhere!

This is a good starting point:
Welcome to the Processing Foundation Discourse << See Guidelines Asking Questions.

It is important to isolate the issue!
Scale it down to an MCVE (see guidelines for this) and this will help isolate it.

That is a common error… and not always so simple to find.

Reference:

I did not get errors in this code block (similar to yours with variables initialized):

Look outside of the code you provided for the source of the error.

:)

import processing.serial.*;
import java.net.*;
import java.io.*;
import java.util.*;
String feed = "http://makezine.com/feed/";
int interval = 5 * 60 * 1000; // retrieve feed every five minutes;
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 =crateFont ("Helvetica", 24);
  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 = millis();
  fetchData();
}
void draw() {
  background(c); 
  int n = (lastTime + interval - millis())/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);
  test(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.lenght()>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-top-date.
    port.clear();
    }
  }
  }
}
void fetchData() {
  // we use these strings to parse the feed 
  String data;
  String chunck;
  // 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 connnection to a buffered
    //reader that reads the data one line at a time.
    BufferedReader in = new
    BufferedReader(new InputStreameReader (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) arduinio=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; // multiply that is made of 4 bytes 
    (ARGB)
    }
  catch(Exeption ex) {//if there was an error, stop the sketch 
  ex.printStackTrace();
  System.out.println("ERROR: "+ex.geMessage());
  }
  }

      ```

Hello @JTR ,

Feedback:

  • This is not formatted properly as per the guidelines.
    This makes it difficult for anyone trying to assist.

Keep in mind that one error can generate another error!
The exploration below was what I did in steps and then all the other errors were showing up one at a time!

  • You did not close the try() block without a closing bracket. Note: may have been ok after fixing other errors!
  • You have a comment that found its way on to the next line. This is close to where the closing bracket should be.

Once you correct the above there are numerous other errors!

  • Incorrect spelling
  • Functions that do not exist
  • Type Serial is ambiguous
  • Cannot find a class or type named “InputStreameReader” < Obvious spelling error!
  • Undeclared variables
  • Code that is commented out that should be on a separate line

Keep plugging away at it!

Correct these first:

  • Spelling errors in code. The code is unforgiving!
  • Code and\or comments on the wrong lines.

Keep in mind that one error can generate another error!
The above list was from what I experienced editing this and you may have to iterate over the code a few times to sort this out.

This is part of the learning curve and a worth wile exercise for you to work through.

I did get it to successfully run without errors.

:)

```import processing.serial.*;
import java.net.*;
import java.io.*;
import java.util.*;
String feed = "http://makezine.com/feed/";
int interval = 5 * 60 * 1000; // retrieve feed every five minutes;
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 = crateFont("Helvetica", 24);
  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 = millis();
  fetchData();
}
void draw() {
  background( c ); 
  int n = (lastTime + interval - millis())/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);
  test(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.lenght()>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-top-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 connnection 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) arduinio=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; // multiply that is made of 4 bytes 
    (ARGB)
    
  }
    catch (Exeption ex) { //if there was an error, stop the sketch 
  ex.printStackTrace();
  System.out.println("ERROR: "+ex.getMessage());
    }
}
   
  

some spelling corrected…

    // 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) arduinio=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; // multiply that is made of 4 bytes 
    (ARGB)
    
  }
    catch (Exeption ex) { //if there was an error, stop the sketch 
  ex.printStackTrace();
  System.out.println("ERROR: "+ex.getMessage());
    }
}
   
here's where I get those errors.

Typographical errors is a better word for this.

Coding oversight, transcription error, mislabeled identifiers are other terms.

Most of errors are not code and errors you introduced.
I am assuming errors are from transcription and not from the source.
Did you enter this manually?

I can’t assist with this as is.

:)

You are right about the learning curve. I think I’m trying to learn Arduino IDE and Processing in a hurry. I was reading the guidelines and it gives the suggestion of writing the code in small chunks. I write it again and check errors. but what do you mean with :

ok, Ill try again thanks.

Take a close look at this:

    //read each line from the feed while ((data = in.readLine())!=null){StringTokenizer st =
    new StringTokenizer(data, "\"<>,.()[]");//break it down while (st.hasMoreTokens())

And this:

    arduino = arduino * 4; // multiply that is made of 4 bytes 
    (ARGB)

There may be others…

Comments in code start with // and are not seen (also grayed out in editor) at run\compile time.

Make sure the code you want is not commented.
Make sure comments are not interpreted as code.

Once you fix that see my previous link to fix this:

import processing.serial.*;

That should get you started. It was a lot easier from there once I corrected the root of the error.

I won’t correct the typographical spelling errors.

You will come out of this better once you work through this and… feel good about it!
You can’t skip over the experience part… it has to be experienced first hand.

:)

How frustating is this Make: Getting Started with Arduino, 3rd Edition somtimes. I don’t have nothing against Processing but why did the author have to include another language without any previous lesson. I was barely learning the Arduino IDE and now Im stuck. I re wrote the sketch corrected some errors but now I have new errors, GREAT!.

The type Serial is ambiguous

The variable “port” does not exist (double-click for suggestions)

The variable “lastTime” does not exist.

etc.

import processing.serial.*;
import java.net.*;
import java.io.*;
import java.util.*;

String feed = "http://makezine.com/feed/";
int interval = 5 * 60 * 1000; // retrieve feed every five minutes;
int lasTime; // the last time we fetched the content

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

int light = 0; // light leveal 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 = createFont("Helvetica",24);
  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 betweeen [ and ] to the number of the port 
  // that you Arduino is connected to. 
  // println(Serial.list());
  String arduinoPort = Serial.list()[0];
  
  port = new Serial(this, arduinoPort, 9600); // connect to Arduino
  
  lastTime = millis();
  fetchData();
}

void draw() {
  background( c );
  int n = (lastTime + interval - millis())/1000;
  
  // Build a colour based on the 3 values
  c = color(peace, love, arduino);
  cs = "#" +  hex(c, 6); // Preapare a string to be sent 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, 280);
  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) { 
    int inByte = port.read(); // check if there is data waitinga // read on byte
    if (inByte != 10) {
      buffer = buffer + char(inByte); // if byte is not newline// 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 
        // lin 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 behing in taking readings 
        // from Arduino. So let's clear the backlog of 
        // incoming sensor readings so the next reaing 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.getInputStreamer()));
   
   // 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) // fund "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; // multipy 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) { 
    ex.printStackTrace(); //if there was an error, stop the sketch 
    System.out.println("ERROR: "+ex.getMessage());
  }
}
    
     
    
    
        
   
    
  
  
  
  

For some reason I had to use port initialized like this to get it to compile:

processing.serial.Serial port;

There are a lot of typos but it can be made to work:

Addendum:

If you do the imports like this you can use just Serial port;

import processing.serial.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

The reason is discussed here:

I changed http to https:

String feed = "https://makezine.com/feed/";

It now works with original code for this example and correction for The type Serial is ambiguous:

There are 5 occurrences of love (find on page) at that link so the 20 is correct!

I was using a VPN before and that generated other connection errors.

I commented this out for testing because I did not have Arduino code and it was blocking:
//port.write(cs); // send data to Arduino

There is a link for “original code for this example” here:
Type Serial is ambiguous - #4 by glv

Seek and you shall find.

The above is on my W10 PC with Processing 4.5.2 and VPN not used.

:)

Guys,

I just wanted to tell you that I finally fixed the sketch. With your help and some AI I could figure out my Errors. I still need to understand what I did but at least I can continue with the Arduino book.

thank you.