Processing-Arduino issue ...

edited October 2013 in Questions about Code

Hi all! I'm pulling data from skyscanner into Processing, checking the flights prices and if there are flights under certain money I want to make Arduino do something. Everything is fine, except one "little" detail : the void draw function doesn't get the value of my integers (the flight prices). I guess this is quite stupid problem, but I don't really know how to solve it. I will appreciate any help ! This is the code:

JSONObject json;
int price1;
int price2;


import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int ledPin = 13;


void setup() {
    println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[8], 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);

  json = loadJSONObject("http:// www.skyscanner.net/db.ashx?m=b&fp=LHR&tp=SOFI&dd=20131208&ucy=UK&lid=en&ccy=GBP&wp=true&rd=20131217&qt=false");

  JSONArray values = json.getJSONArray("rs"); // get the first array
  JSONArray values2 = json.getJSONArray("ds"); // get the second array

  for (int i = 0; i < values.size(); i++) {

      JSONObject rs = values.getJSONObject(i); 
      JSONObject ds = values2.getJSONObject(i); 
      price1 = rs.getInt("p");
      price2 = ds.getInt("p");


      if (price1 < 100) { //check if the price is less than £100 and print it out if so
      print(price1  + "," );
     } 

      if (price2 < 100) { //check if the price is less than £100 and print it out if so
       print(price2  + "," );
     } 
}


void draw()
{
  if (price1 < 100) {  // check if the price is less than £100 and make Arduino to blink if so
        arduino.digitalWrite(ledPin, Arduino.HIGH);
        delay(100);
        arduino.digitalWrite(ledPin, Arduino.LOW);
        delay(1000);
   } 


  if (price2 < 100) { // check if the price is less than £100 and make Arduino to blink if so
        arduino.digitalWrite(ledPin, Arduino.HIGH);
        delay(100);
        arduino.digitalWrite(ledPin, Arduino.LOW);
        delay(1000);
    }  

}

Answers

  • The Arduino code is only confusing things... Here is your code without this part:

    JSONObject json;
    int price1;
    int price2;
    
    
    void setup() {
    
      json = loadJSONObject("http://www.skyscanner.net/db.ashx?m=b&fp=LHR&tp=SOFI&dd=20131208&ucy=UK&lid=en&ccy=GBP&wp=true&rd=20131217&qt=false");
    
      JSONArray values = json.getJSONArray("rs"); // get the first array
      JSONArray values2 = json.getJSONArray("ds"); // get the second array
    
      println("v1 " + values);
      println("v2 " + values2);
    
      for (int i = 0; i < values.size(); i++) {
    
        JSONObject rs = values.getJSONObject(i);
        JSONObject ds = values2.getJSONObject(i);
        price1 = rs.getInt("p");
        price2 = ds.getInt("p");
    
    
        if (price1 < 100) { //check if the price is less than £100 and print it out if so
          print(price1  + "," );
        }
    
        if (price2 < 100) { //check if the price is less than £100 and print it out if so
          print(price2  + "," );
        }
      }
    }
    
    void draw()
    {
      if (price1 < 100) {  // check if the price is less than £100 and make Arduino to blink if so
        println("Draw 1 " + price1);
      }
    
    
      if (price2 < 100) { // check if the price is less than £100 and make Arduino to blink if so
        println("Draw 2 " + price2);
      }
    }
    

    Apparently, the p item isn't always here, I get an error about object not found. You have to check for that.

  • Hi PhiLho ! Thanks for your answer. The arduino code connects processing with Arduino, so you can do everything through processing. The problem is not solved in your code example, void draw still doesn't do anything, I mean doesn't print the prices.

  • Read my comment below the code... Take a look at the Json data itself. Json objects are not always the same in the response, so you need to check if an object is there or not.

Sign In or Register to comment.