nizzie12 on DeviantArthttps://www.deviantart.com/nizzie12/art/Arduino-and-Processing-Temp-display-608650916nizzie12

Deviation Actions

nizzie12's avatar

Arduino and Processing Temp display

By
Published:
248 Views

Description

Code:
int rx_byte;            // stores the byte received on the serial port
int red_LED = 6;        // LED pin numbers on Arduino
int grn_LED = 4;
int amb_LED = 2;

void setup()
{
   Serial.begin(9600);
   pinMode(red_LED, OUTPUT);      // LED pins
   pinMode(grn_LED, OUTPUT);
   pinMode(amb_LED, OUTPUT);
   digitalWrite(red_LED, LOW);    // switch LEDs off
   digitalWrite(grn_LED, LOW);
   digitalWrite(amb_LED, LOW);
}

void loop()
{
   float temperature = 0.0;   // stores the calculated temperature
   int sample;                // counts through ADC samples
   float ten_samples = 0.0;   // stores sum of 10 samples
   
   if (Serial.available() > 0) {
       rx_byte = Serial.read();
       // received bytes that will switch one of the LEDs on
       if (rx_byte == 'R') {
           digitalWrite(red_LED, HIGH);
       }
       else {
           digitalWrite(red_LED, LOW);
       }
       if (rx_byte == 'G') {
           digitalWrite(grn_LED, HIGH);
       }
       else {
           digitalWrite(grn_LED, LOW);
       }
       if (rx_byte == 'A') {
           digitalWrite(amb_LED, HIGH);
       }
       else {
           digitalWrite(amb_LED, LOW);
       }
   }
   // read the temperature from A0 and convert it into a floating point number
   // send the temperature over the USB port after conversion
   else {
       // take 10 samples from the MCP9700
       for (sample = 0; sample < 10; sample++) {
           // convert A0 value to temperature
           temperature = ((float)analogRead(A0) * 5.0 / 1024.0) - 0.5;
           temperature = temperature / 0.01;
           // sample every 0.1 seconds
           delay(100);
           // sum of all samples
           ten_samples = ten_samples + temperature;
       }
       // get the average value of 10 temperatures
       temperature = ten_samples / 10.0;
       // send temperature out of serial port
       Serial.println(temperature);
       ten_samples = 0.0;
   }
}
Image size
750x1334px 209.07 KB
Date Taken
Apr 5, 2016, 9:34:33 PM
© 2016 - 2024 nizzie12
Comments0
Join the community to add your comment. Already a deviant? Log In