Hardware Reference
In-Depth Information
{
digitalWrite(LED, HIGH); //Turn the LED on
if (millis() % 1000 == 0) //If time is multiple
//of 1000ms
{
int temperature = analogRead(TEMP); //Read the temperature
int brightness = analogRead(LIGHT); //Read the light level
Keyboard.print(counter); //Print the index number
Keyboard.print(","); //Print a comma
Keyboard.print(temperature); //Print the temperature
Keyboard.print(","); //Print a comma
Keyboard.println(brightness); //Print brightness, newline
counter++; //Increment the counter
}
}
else
{
digitalWrite(LED, LOW); //If logger not running, turn LED off
}
}
/*
* Debouncing Function
* Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //If it's different…
{
delay(5); //Wait 5ms
current = digitalRead(BUTTON); //Read it again
}
return current; //Return the current
//value
}
Before you test the data logger, let's highlight some of the new functionality
that has been implemented in this sketch. Similarly to how you initialized the
serial communication, the keyboard communication is initialized by putting
Keyboard.begin() in the setup() .
Each time through loop() , the Arduino checks the state of the button and runs
the debouncing function that you are already familiar with. When the button
is pressed, the value of the running variable is inverted. This is accomplished
by setting it to its opposite with the ! operator.
While the Arduino is in running mode, the logging step is executed only every
1000ms using the logic described previously. The keyboard functions work very
similarly to the serial functions. Keyboard.print() “types” the given string into
Search WWH ::




Custom Search