반응형

【 Leonardo #1 Let's collect Temperature & Brightness value to PC!

Welcome to RasINO~!   
In this chapter, we will learn how to use Arduino Leonardo board.    Let's go~!

▶ The Goal : 
 1. You can see how to use the Leonardo board.
 2. You can transfer the data from the temperature sensor & optical sensor (CDS) to the PC.
 3. You can make your data into Excel charts or use them in a variety of ways.

▶ Connection diagram :
  ( Click image to enlarge )

 

▶ Coding and annotation : 
/* Leonardo board Transfer temperature & brightness data to PC with Leonardo board */

#include <Keyboard.h>   
const int TEMP = 0;    // A0 port 
const int CDS = 1;    // A1 port  
const int LED = 12; 
const int BUTTON = 11; 
boolean lastBtn = LOW; 
boolean currentBtn = LOW; 
boolean running = false; 
int counter = 1;

void setup ( ) {  
  pinMode(LED, OUTPUT);
  Keyboard.begin();
}

void loop ( )  {
  currentBtn = debounce (lastBtn); 
  if (lastBtn == LOW && currentBtn == HIGH) // Click the button
  { 
     running = !running;   // Change state values backwards
   } 
  lastBtn = currentBtn; // Update button state values 
  if (running) // Writing data
  { 
     digitalWrite(LED, HIGH); 
     // The 'millis()' function returns the data in ms. 
     // So this function is used when the 'delay()' function is not available 
   if (millis()%1000 == 0) // Execute if statement every second
   { 
      int temperature = analogRead(TEMP); 
      int  brightness = analogRead(CDS);
      float mVoltage = temperature*5000.0/1024.0; // Converted to Celsius temperature 
      float TempDotC = (mVoltage - 500) / 10.0;   // Converted to Celsius temperature 
      Keyboard.print(counter);  // Data numbering before each line 
      Keyboard.print(",");
      Keyboard.print(TempDotC);  // Temperature data
      Keyboard.print(",");
      Keyboard.print(brightness);   // Brightness data
      Keyboard.print("\n");
      counter++;
    }     
  } 
  else 
  { 
    digitalWrite(LED, LOW); 
  } 
}

// Generate subfunctions to prevent button bouncing 
boolean debounce (boolean last) 
{ 
  boolean current = digitalRead (BUTTON); 
  if(last != current) 
  { 
    delay(5); 
    current = digitalRead(BUTTON); 
  } 
  return current;     
}

▶ Code download : 
( The code is zip compressed.)

Eng_TMP36_CDS_to_PC.zip
0.00MB

 

▶ Video lecture :  
(Look in full screen view)

 

(You can watch it on YouTube.)
https://youtu.be/K-Dc5MqBYaI

 

반응형