fichiers processing2 et arduino : GS

P5-2 :

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

String lignes[];
String indice;
float  oldIndice =0;
int  pos = 0;
float  newIndice;

Arduino arduino;
int ledPin = 13;

void setup()
{

println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[1], 57600);//changer au besoin : [1]
arduino.pinMode(ledPin, Arduino.OUTPUT);

lignes = loadStrings("http://finance.yahoo.com/q;_ylt=Aodjo50Oeevvu0HbU4Vy8Lonv7gF;_ylc=X1MDMjE0MjQ3ODk0OARfcgMyBGZyA3VoM19maW5hbmNlX3dlYl9ncwRmcjIDc2EtZ3AEZ3ByaWQDBG5fZ3BzAzEwBG9yaWdpbgNmaW5hbmNlLnlhaG9vLmNvbQRwb3MDMQRwcXN0cgMEcXVlcnkDR1MsBHNhYwMxBHNhbwMx?p=http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3DGS%26ql%3D0&type=2button&fr=uh3_finance_web_gs&uhb=uhb2&s=GS");

for (int i = 0; i < lignes.length; i++) {
if (lignes[i].contains("yfs_l84_gs")) {
pos = i;
println(pos);}}

}

void draw()
{

background(255);
fill(0);
lignes = loadStrings("http://finance.yahoo.com/q;_ylt=Aodjo50Oeevvu0HbU4Vy8Lonv7gF;_ylc=X1MDMjE0MjQ3ODk0OARfcgMyBGZyA3VoM19maW5hbmNlX3dlYl9ncwRmcjIDc2EtZ3AEZ3ByaWQDBG5fZ3BzAzEwBG9yaWdpbgNmaW5hbmNlLnlhaG9vLmNvbQRwb3MDMQRwcXN0cgMEcXVlcnkDR1MsBHNhYwMxBHNhbwMx?p=http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3DGS%26ql%3D0&type=2button&fr=uh3_finance_web_gs&uhb=uhb2&s=GS");
indice = lignes[pos].substring(lignes[pos].indexOf("yfs_l84_gs") + 12, lignes[pos].indexOf("</span></span>"));

/* text("Indice GS : " + indice, width*0.15, height*0.66);
text("oldIndice GS : " + oldIndice, width*0.15, height*0.70);
*/
println("Indice GS : " + indice+" / oldIndice GS : " + oldIndice);
float newIndice = float(indice);
println("int"+indice);
if (oldIndice == newIndice){
delay(1000);
oldIndice = newIndice;}
else{
println("---");
arduino.digitalWrite(ledPin, Arduino.HIGH);
delay(10);
arduino.digitalWrite(ledPin, Arduino.LOW);
oldIndice = newIndice;}

}

arduino :

//set up L293D
int motor1Pin1 = 6;    // pin 2 (Input 1)  du L293D
int motor1Pin2 = 8;    // pin 7 (Input 2) du L293D
int enablePin = 10;     // pin 1 (Enable 1) du L293D
//set up vitesse ventilo
int ventiloSpeed = 0;
int newVentiloSpeed=0;
int time = 200;
//set up serial
float incomingByte=162.4;
float oldIncomingByte=incomingByte;
int retourTaux =0;
int x=1;
//set up LCD
#include <LiquidCrystal.h>
#include <stdio.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the number of columns and rows on the LCD

lcd.begin(16, 2);

Serial.begin(9600);

pinMode(motor1Pin1, OUTPUT);//controle un sens
//pinMode(motor1Pin2, OUTPUT);//controle l'autre sens
pinMode(enablePin, OUTPUT);
pinMode(13, OUTPUT);
//commande par port serie manuellement

/* while (Serial.available() < 0) {
//On lit le byte qui arrive sur le port série
oldIncomingByte = Serial.read();
delay(10);
};

// Mettre la broche Enable a high comme ca le moteur tourne

for (int i=0; i<=ventiloSpeed; i++){
analogWrite(motor1Pin1, i);   // mettre pin 2 a 293D low
delay(time);
}
;*/
digitalWrite(enablePin, HIGH);

}

void loop() {
/*int serieDispo = Serial.available();
while (serieDispo < 0) {
//On lit le byte qui arrive sur le port série
incomingByte = Serial.read();
};*/
//commande par port serie manuellement
if (Serial.available()> 0) {

char valeur = Serial.read();

switch(valeur) {
case'a' :
incomingByte = incomingByte + 0.1;
Serial.print("incomingByte");
Serial.println(incomingByte);
Serial.print("oldIncomingByte");
Serial.println(oldIncomingByte);
break;
case'b' :
incomingByte = incomingByte - 0.1;
Serial.print ("incomingByte");
Serial.println(incomingByte);
Serial.print("oldIncomingByte");
Serial.println(oldIncomingByte);
break;
case'd' :
Serial.print(":)");
break;
default :
Serial.print("???");
};
//calcule difference entre ancien et nouveau taux
if (incomingByte != oldIncomingByte){
{
retourTaux = int ((incomingByte - oldIncomingByte)*100);
Serial.print("retour");
Serial.println(retourTaux);
//calcule nouvelle vitesse

newVentiloSpeed = ventiloSpeed + (retourTaux*5);
if (newVentiloSpeed <0){newVentiloSpeed=0;};
if (newVentiloSpeed >250){newVentiloSpeed=250;};
Serial.print("ventilo");
Serial.println(ventiloSpeed);
Serial.print("newVentilo");
Serial.println(newVentiloSpeed);
//verification si difference est possitive ou negative
if (retourTaux>0){
x=1;
}
else{
x=-1;
};
//variasation progressive du changement de vitesse

while (newVentiloSpeed != ventiloSpeed){
ventiloSpeed = ventiloSpeed + x;
analogWrite(motor1Pin1, ventiloSpeed);
Serial.print("chanegement ventilo");
Serial.println(newVentiloSpeed);
Serial.print("ventilo actuel");
Serial.println(ventiloSpeed);
};
//revalorisation des variables
newVentiloSpeed = ventiloSpeed;
oldIncomingByte = incomingByte;
};
}
}
//LCD
char str[6];
lcd.setCursor(0, 0);
lcd.print("Goldman S.");
// set the cursor to column 0, line 1
// line 1 is the second row, since counting begins with 0
lcd.setCursor(11, 0);
// print to the second line
// faire recherche    sprintf(str,"%5.1f",3.51); //incomingByte);
lcd.print(incomingByte);
lcd.setCursor(0, 1);
lcd.print("Ventillo.");
// set the cursor to column 0, line 1
// line 1 is the second row, since counting begins with 0
lcd.setCursor(12, 1);
// print to the second line

sprintf(str,"%4d",ventiloSpeed);
lcd.print(str);

//fin
}