Hardware Reference
In-Depth Information
The blink() method is just like ones
you've seen previously in this topic.
It blinks an LED to indicate that setup
is over.
8
void blink(int thisPin, int howManyTimes) {
// Blink the LED:
for (int blinks=0; blinks< howManyTimes; blinks++) {
digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}
The main loop handles incoming
serial data, reads the potentiometer,
and sends out da if there's a sufficient
change in the potentiometer's reading.
8
void loop() {
// listen for incoming serial data:
if (Serial.available() > 0) {
handleSerial();
}
// listen to the potentiometer:
int sensorValue = readSensor();
// if there's something to send, send it:
if (sensorValue > 0) {
Serial.println(sensorValue, DEC);
}
}
Two other methods are called
from the loop: handleSerial() , which
listens for strings of ASCII numerals
and converts them to bytes in order to
set the brightness of the LED on the
PWM output; and readSensor() , which
reads the potentiometer and checks
to see whether the change on it is high
enough to send the new value out via
radio. Here are those methods.
8
void handleSerial() {
char inByte = Serial.read();
// save only ASCII numeric characters (ASCII 0 - 9):
if (isDigit(inByte)){
inputString = inputString + inByte;
}
// if you get an ASCII newline:
if (inByte == '\n') {
// convert the string to a number:
int brightness = inputString.toInt();
// set the analog output LED:
analogWrite(analogLed, brightness);
// clear the input string for the
// next value:
inputString = "";
Serial.print(brightness);
}
}
int readSensor() {
int result = analogRead(sensorPin);
ยป
 
Search WWH ::




Custom Search