Différences entre versions de « Afficheur LCD-part 2-Code Arduino »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 1 : Ligne 1 :
 
{{afficheur-lcd-part2-nav}}
 
{{afficheur-lcd-part2-nav}}
  
The sketch for this is based on that of lesson 11. Load it up onto your Arduino and you should find that warming the temperature sensor by putting your finger on it will increase the temperature reading.
+
Le sketch est basé sur le [[Afficheur LCD|tutoriel précédent]].  
  
Also if you wave your hand over the photocell blocking out some of the light, the reading will decrease.
+
Vous noetez probablement les modifications (voir ci-dessous).
 +
 
 +
Chargez le sur votre Arduino et vous devriez voir la température s'afficher.
 +
 
 +
En maintenant le senseur entre vos doigts, vous devriez voir la température s'élever.
 +
 
 +
Si vous agitez/placez vos mains au dessus de la photo-résistance (bloquant ainsi une partie de la lumière), la valeur afficher à l'écran devrait également diminuer.
  
 
  <nowiki>
 
  <nowiki>
 
     /*
 
     /*
     Adafruit Arduino - Lesson 12. Light and Temperature
+
     www.MCHobby.be - Lecture de la temperature et lumiere
 +
                    avec Afficheur LCD.
 +
    Source:
 +
      Adafruit Arduino - Lesson 12. Light and Temperature  
 +
      Transalted by MCHobby with authorization of Adafruit Industries
 
     */
 
     */
 
      
 
      
Ligne 15 : Ligne 25 :
 
     int lightPin = 1;
 
     int lightPin = 1;
 
      
 
      
     // BS E D4 D5 D6 D7
+
     //               BS E D4 D5 D6 D7
     LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
+
     LiquidCrystal lcd( 7, 8, 9, 10, 11, 12);
 
      
 
      
 
     void setup()
 
     void setup()
 
     {
 
     {
    lcd.begin(16, 2);
+
      lcd.begin(16, 2);
 
     }
 
     }
 
      
 
      
 
     void loop()
 
     void loop()
 
     {
 
     {
    // Display Temperature in C
+
      // Afficher la température en degres Celcius
    int tempReading = analogRead(tempPin);
+
      int tempReading = analogRead(tempPin);
    float tempVolts = tempReading * 5.0 / 1024.0;
+
      float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 100.0;
+
      float tempC = (tempVolts - 0.5) * 100.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
+
      float tempF = tempC * 9.0 / 5.0 + 32.0;
    // ----------------
+
      // ----------------
    lcd.print("Temp F ");
+
      lcd.print("Temp F ");
    lcd.setCursor(6, 0);
+
      lcd.setCursor(6, 0);
    lcd.print(tempF);
+
      lcd.print(tempF);
    // Display Light on second row
+
     
    int lightReading = analogRead(lightPin);
+
      // Display Light on second row
    lcd.setCursor(0, 1);
+
      int lightReading = analogRead(lightPin);
    // ----------------
+
      lcd.setCursor(0, 1);
    lcd.print("Light ");
+
      // ----------------
    lcd.setCursor(6, 1);
+
      lcd.print("Light ");
    lcd.print(lightReading);
+
      lcd.setCursor(6, 1);
    delay(500);
+
      lcd.print(lightReading);
 +
      delay(500);
 
     }
 
     }
 
</nowiki>
 
</nowiki>

Version du 3 janvier 2013 à 11:36

Le sketch est basé sur le tutoriel précédent.

Vous noetez probablement les modifications (voir ci-dessous).

Chargez le sur votre Arduino et vous devriez voir la température s'afficher.

En maintenant le senseur entre vos doigts, vous devriez voir la température s'élever.

Si vous agitez/placez vos mains au dessus de la photo-résistance (bloquant ainsi une partie de la lumière), la valeur afficher à l'écran devrait également diminuer.

    /*
    www.MCHobby.be - Lecture de la temperature et lumiere
                     avec Afficheur LCD.
    Source:
      Adafruit Arduino - Lesson 12. Light and Temperature    
      Transalted by MCHobby with authorization of Adafruit Industries
    */
     
    #include <LiquidCrystal.h>
     
    int tempPin = 0;
    int lightPin = 1;
     
    //                BS  E D4  D5  D6  D7
    LiquidCrystal lcd( 7, 8, 9, 10, 11, 12);
     
    void setup()
    {
       lcd.begin(16, 2);
    }
     
    void loop()
    {
       // Afficher la température en degres Celcius
       int tempReading = analogRead(tempPin);
       float tempVolts = tempReading * 5.0 / 1024.0;
       float tempC = (tempVolts - 0.5) * 100.0;
       float tempF = tempC * 9.0 / 5.0 + 32.0;
       // ----------------
       lcd.print("Temp F ");
       lcd.setCursor(6, 0);
       lcd.print(tempF);
       
       // Display Light on second row
       int lightReading = analogRead(lightPin);
       lcd.setCursor(0, 1);
       // ----------------
       lcd.print("Light ");
       lcd.setCursor(6, 1);
       lcd.print(lightReading);
       delay(500);
    }

I find it useful to put a comment line above the 'lcd' command.

//                BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

This makes things easier if you decide to change which pins you use.

In the 'loop' function there are now two interesting things going on. Firstly we have to convert the analog from the temperature sensor into an actual temperature, and secondly we have to work out how to display them.

First of all, lets look at calculating the temperature.

    int tempReading = analogRead(tempPin);
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 100.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;

The raw reading from the temperature sensor is first multiplied by 5 and then divided by 1024 to give us the voltage (between 0 and 5) at the 'tempPin' analog input.

To convert the voltage coming from the TMP36 into a temperature in degrees C, you have to subtract 0.5V from the measurement and then multiply by 100.

To convert this into a temperature in Fahrenheit, you then have to multiply it by 9/5 and then add 32.

Displaying changing readings on an LCD display can be tricky. The main problem is that the reading may not always be the same number of digits. So, if the temperature changed from 101.50 to 99.00 then the extra digit from the old reading is in danger of being left on the display.

To avoid this, write the whole line of the LCD each time around the loop.

    // ----------------
    lcd.print("Temp F ");
    lcd.setCursor(6, 0);
    lcd.print(tempF);

The rather strange comment serves to remind you of the 16 columns of the display. You can then print a string of that length with spaces where the actual reading will go.

To fill in the blanks, set the cursor position for where the reading should appear and then print it.

Exactly the same approach is used for displaying the light level. There are no units for the light level, we just display the raw reading from the analog read.

Créer par Simon Monk pour AdaFruit

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.