Nedenstående sketch er lavet for National Semiconductor’s LM35DZ, i dag ejet af Texas Instruments (TI). Eller anden temperatur sensor med 0°C er 0V og ellers 10mV pr grad celsius.
Ja, som det kan ses af nedenstående, så skriver jeg nogle gange på Engelsk.
Teksten til højre for // er en kort beskrivelse af hvad hver linje gør.
/* * Temperatur read out on 4 Digit 7-Segment Serial Display. * * Clearing the Display and setting the brightness level. * Test Digit placing. * Test of each segment in each digit. * Test of each Decimal Point, Colon and Apostrophe. * Making Temperatur conversion to Display for 0-100°C at 10mV/°C. */ int analogValue0 = 0; // Variable for analog value on A0 int outputValue0 = 0; // Making mapping possible for A0 void setup() // Run once, when the sketch starts { Serial.begin(9600); // Setting communication up at 9600 bps on TX pin at Arduino Uno Serial.write(0x76); // Clear the display and set for Digit 1 delay(500); // Delay for clearing the Display Serial.write(0x7A); // Command for brightness Serial.write(0x01); // Brightness level delay(10); // Delay for changing brightness Serial.write("1234"); // Display read out 1234 delay(500); // Time for Display read out Serial.write("8888"); // Display read out 8888 delay(800); // Time for Display read out Serial.write(0x76); // Clear the display and set for Digit 1 delay(10); // Delay for clearing the Display Serial.write(0x77); // Command for decimal point Serial.write(0x01); // Display read out decimal point digit 1 delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x02); // Display read out decimal point digit 2 delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x10); // Display read out Time Colon delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x04); // Display read out decimal point digit 3 delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x20); // Display read out Apostrophe delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x08); // Display read out decimal point digit 4 delay(300); // Time for Display read out Serial.write(0x77); // Command for decimal point Serial.write(0x40); // Switch off decimal point Serial.write(0x77); // Command for decimal point Serial.write(0x04); // Display read out decimal point digit 3 } void loop() // Run over and over again { analogValue0 = analogRead(0); // Reading of the analog input pin A0 Serial.write(0x78); // Blank Digit 1 outputValue0 = map(analogValue0, 0, 1023, 0, 5000); // 10mV pr. °C (5V = 500°C) if (outputValue0 < 100) Serial.write(0x78); // Blank Digit 2 Serial.print(outputValue0); // * * * Temp value * * * delay(10000); //Delay before next conversion / read out }
Den opmærksomme læser vil nok bemærke, at der ingen midling er af temperaturen.
Grunden til at map går op til 5000 er, at så får vi en decimal på.
Med if < 100 skulle der hermed være sikret at der ikke f.x. skrives 99, men 9,9 når temperaturen kommer under 10 grader.