Back to Parent

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

Arduino arduino;

JSONObject json;
String url = "https://www.kimonolabs.com/api/2g5uthv0?apikey=kjEf4Id56yxY1nf4pFmDY8xoi7r41g6u";
int fan1Pin = 9;
int dirPin = 2;
int stepPin = 3;
float magnitude = 0;
String direction = "";
int degree = 0;
int numFan = 0;
int mag = 0;
int deg_old = 0;
int deg_new = 0;
int stepstaken = 0;
long time = millis();

void setup(){
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[9], 57600);
  arduino.pinMode(fan1Pin, Arduino.OUTPUT);
  arduino.pinMode(dirPin, Arduino.OUTPUT); 
  arduino.pinMode(stepPin, Arduino.OUTPUT);  
}

void draw(){
  if((time-millis()) >= 5000 || millis() <= 5000){
    loadData();
    correction();
    println("mag " +mag);
    println("degree " +degree);
    arduino.digitalWrite(fan1Pin, 255); //mag
    rot();
  }
  delay(10000);
  direction = "N";
  correction();
  rot();
  
  delay(10000);
  direction = "NE";
  correction();
  rot();
  
  delay(10000);
  direction = "E";
  correction();
  rot();
}

void rot(){
  //rotate a specific degree
    if (deg_old < degree){
      deg_new= degree - deg_old;
      rotateDeg(deg_new); }
    if (deg_old == degree){
      deg_new = degree;} 
    if (deg_old > degree){
      deg_new= deg_old-degree;
      rotateDeg(deg_new); }
  
    deg_old = degree;
    
  }
    
void keyPressed() {
   rotateDeg(-stepstaken);
   arduino.analogWrite(fan1Pin, 0);
   println("STOPPED");
}

void rotateDeg(float deg){ 
  int dir = (deg < 0)? Arduino.HIGH:Arduino.LOW;
  arduino.digitalWrite(dirPin,dir); 

  int step = int(abs(deg)*4.44); //specific to stepper motor

  for(int g=0;g<step;g++){
    arduino.digitalWrite(3,Arduino.HIGH);
    delay(1); 
  
    arduino.digitalWrite(3,Arduino.LOW);
    delay(1);
    }
  time = millis();
  stepstaken += deg;
}  

void loadData(){
  json = loadJSONObject(url);
  print(json);
  JSONObject results = json.getJSONObject("results");
  
  //get the array collection 1
  JSONArray jsonWind = results.getJSONArray("collection1");
  int numWind = jsonWind.size();
  for (int i=0; i < numWind; i++){
    JSONObject tempWind = jsonWind.getJSONObject(i);
    magnitude = tempWind.getFloat("magnitude");
    println("magnitude " +magnitude);
    direction = tempWind.getString("direction");
    println("direction " +direction);
  }
}
  
void correction(){
  if (direction.equals("N")){
  degree = 0;}
   if (direction.equals("ENE")){
  degree = 22;}
  if (direction.equals("NE")){
  degree = 45;}
  if (direction.equals("ENE")){
  degree = 22*3;}
  if (direction.equals("E")){
  degree = 90;}
  if (direction.equals("ESE")){
  degree = 22*5;}
  if (direction.equals("SE")){
  degree = 135;}
  if (direction.equals("SSE")){
  degree = 22*7;}
  if (direction.equals("S")){
  degree = 180;}
  if (direction.equals("SSW")){
  degree = 22*9;}
  if (direction.equals("SW")){
  degree = 225;}
  if (direction.equals("WSW")){
  degree = 22*11;}
  if (direction.equals("W")){
  degree = 270;}
  if (direction.equals("WNW")){
  degree = 22*13;}
  if (direction.equals("NW")){
  degree = 315;}
  if (direction.equals("NNW")){
  degree = 22*15;}
  
  //mag = (int)map(magnitude, 0, 30, 75, 255);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0