Created
June 23, 2012 23:05
-
-
Save sulram/2980487 to your computer and use it in GitHub Desktop.
Oficina ARDUINO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int led1 = 13; | |
int led2 = 10; | |
int triggerPin = 4; | |
int echoPin = 3; | |
int vento = 6; | |
boolean led1on = false; | |
boolean led2on = false; | |
long m1 = 0; | |
long m2 = 0; | |
int ciclo1 = 1000; | |
int ciclo2 = 1000; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
Serial.begin(9600); | |
// initialize the digital pin as an output. | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(triggerPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(vento, OUTPUT); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
long m = millis(); | |
if( m - m1 > ciclo1 ){ | |
m1 = m; | |
if(led1on) { | |
digitalWrite(led1,LOW); | |
//led1on = false; | |
} | |
else { | |
digitalWrite(led1,HIGH); | |
//led1on = true; | |
} | |
led1on = !led1on; | |
} | |
if( m - m2 > ciclo2 ){ | |
m2 = m; | |
if(led2on) { | |
digitalWrite(led2,LOW); | |
//led1on = false; | |
} | |
else { | |
digitalWrite(led2,HIGH); | |
//led1on = true; | |
} | |
led2on = !led2on; | |
/*ciclo2 = ciclo2 - 50; | |
if(ciclo2 < 50){ | |
ciclo2 = 1000; | |
}*/ | |
} | |
digitalWrite(triggerPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(triggerPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(triggerPin, LOW); | |
long duration = pulseIn(echoPin, HIGH); | |
float cm = duration / 58; | |
if( cm < 30 ){ | |
Serial.print("cm:"); | |
Serial.print('\t'); | |
Serial.print(cm); | |
Serial.print('\t'); | |
Serial.print("vento:"); | |
Serial.print('\t'); | |
Serial.print(255-255*(cm/30.0)); | |
Serial.print('\t'); | |
Serial.print("ciclo:"); | |
Serial.print('\t'); | |
Serial.println(1000*30/cm); | |
analogWrite(vento, 255 - 255 * (cm/30.0)); | |
ciclo2 = 1000 / cm; | |
} | |
delay(10); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
Serial arduino; // The serial port | |
void setup () { | |
size(800, 600); | |
println(Serial.list()); | |
arduino = new Serial(this, Serial.list()[0], 9600); | |
arduino.bufferUntil('\n'); | |
noStroke(); | |
background(0); | |
} | |
void draw () { | |
// everything happens in the serialEvent() | |
} | |
void serialEvent (Serial device) { | |
String string_arduino = device.readStringUntil('\n'); | |
if (string_arduino != null) { | |
string_arduino = trim(string_arduino); | |
float val = map( int(string_arduino), 50, 250, 0, 1023 ); | |
//background( val / 4 ); | |
fill( val/4 , 20 ); | |
rect(0,0,width,height); | |
fill( 255-val/4); | |
ellipse( width / 2, height / 2, val, val); | |
println(string_arduino); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define triggerPin 4 | |
#define echoPin 3 | |
void setup() { | |
// Configura a saída serial para recuperar a leitura do sensor | |
Serial.begin(9600); | |
// Configura a porta digital em modo saida | |
pinMode(triggerPin, OUTPUT); | |
// Configura a porta digital em modo entrada | |
pinMode(echoPin, INPUT); | |
} | |
void loop() { | |
// Envia um sinal de 10ms ao sensor | |
digitalWrite(triggerPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(triggerPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(triggerPin, LOW); | |
// O sensor calcula o tempo gasto entre o envio e o recebimento | |
// do sinal e retorna um pulso com esta duração | |
long duration = pulseIn(echoPin, HIGH); | |
// Converte o tempo para distancia em centimetros | |
float cm = microsecondsToCentimeters(duration); | |
// Informa a distancia na serial | |
Serial.println(cm); | |
delay(10); | |
} | |
float microsecondsToCentimeters(long microseconds){ | |
// Converte o tempo de microsegundos para segundos | |
float seconds = (float) microseconds / 1000000.0; | |
// Como a velocidade do som de 340m/s calcula-se a | |
// distancia percorrida | |
float distance = seconds * 340; | |
// Divide o resultado por dois pois o tempo é calculado | |
// considerando a ida e a volta do sinal | |
distance = distance / 2; | |
// Converte o resultado em metros para centimetros | |
distance = distance * 100; | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment