No terceiro encontro, realizado no auditório do CCSL do IME-USP, nós fizemos duas práticas, sendo uma com LED e potenciômetro, e outra utilizando um sensor ultrassônico.
Na primeira, controlamos a luminosidade de um LED com um potenciômetro. A luminosidade de um LED não se controla (muito bem) através da variação da corrente que passa por ele, então resolveu-se variar o tempo no qual o LED permaneceria aceso dentro de um intervalo de tempo de 20ms a fim de controlar a luminosidade do mesmo. Segue abaixo o código utilizado:
/* Pot sketch blink an LED at a rate set by the position of a potentiometer */
const int potPin = 0; // Select the input pin for the potentiometer
const int ledPin = 13; // Select the pin for the LED
int val = 0; // Variable to store the value coming from the sensor
void setup ()
{
pinMode(ledPin, OUTPUT); // Declare the ledPin as an OUTPUT
}
void loop ()
{
//val = analogRead(potPin)/50;
val = map(analogRead(potPin), 0, 1023, 0, 20);
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);
delay(21-val);
}
Na outra prática utilizamos um sensor ultrassônico. Com ele é possível controlar a distância entre um objeto e o sensor até um certo limite. No nosso teste limitamos para 20cm, de modo que ele só identificaria algum obstáculo nesta distância. Discutimos sobre a função pulseIn para entender melhor a leitura realizada. Também fizemos alguns testes com limites maiores do que 20cm, encontrando alguns resultados estranhos. Segue abaixo o código utilizado:
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified
by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
and modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200 ; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup ()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop ()
{
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2; // Calculate the distance (in cm) based on the speed of sound.
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
} else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(distance);
Serial.print(" ");
Serial.println(duration);
digitalWrite(LEDPin, LOW);
}
delay(50); // Delay 50ms before next reading.
}