Help please! ARDUINO and PROCESSING temperature sensor code

edited February 2018 in Arduino

Hi all,

so for a project with school i had created a temperature sensor using an arduino and breadboard circuit i will leave my code below for the arduino. My issue is that i cannot receive the data from the arduino using processing. I want to create a temperature graph to show on processing.

Arduino code:

#include <LiquidCrystal.h>

int tempPin = 0;
int lightPin = 1;

/*const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);*/

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int value=0;            
float volts=0.0;      
float temp=0.0;      
float tempF=0.0;
int sensorPin = A0;

void setup() 
{
  pinMode(3,INPUT);      //setting arduino pin3 as input
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop()
{
Serial.println(analogRead(0));
 delay(50); 

  //tempReading = analogRead(tempPin);

  int tempReading = analogRead(A0);
  Serial.write(tempReading);
  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("TempC             ");
  lcd.setCursor(6, 0);
  lcd.print(tempC);

  int lightReading = analogRead(lightPin);
  lcd.setCursor(0, 1);

  lcd.print("TempF              ");  
  lcd.setCursor(6, 1);
  lcd.print(tempF);
  Serial.print(tempC);
  Serial.println(tempF);
  delay(500);

}


Processing code:
import processing.serial.*;
//import cc.arduino.*;
//Arduino arduino;



//init variables
Serial commPort;
int val;
float tempC;
float tempF;
//float tempVolts;
int yDist;
float[] tempHistory = new float[100];

void setup()
{
 //setup fonts for use throughout the application
//set the size of the window
 size(900,450);
//init serial communication port
 commPort = new Serial(this, Serial.list()[0], 9600);

//fill tempHistory with default temps
 for(int index = 0; index<100; index++)
 tempHistory[index] = 0;
}

void draw()
{
 //get the temp from the serial port
 while (commPort.available() > 0) 
 {
 tempC = commPort.read();
//refresh the background to clear old data
 background(123);

 //draw the temp rectangle
 colorMode(RGB, 100);  //use color mode sized for fading
 stroke (0);
 rect (49,19,22,162);
 //fade red and blue within the rectangle
 for (int colorIndex = 0; colorIndex <= 160; colorIndex++) 
 {
 stroke(160 - colorIndex, 0, colorIndex);
 line(50, colorIndex + 20, 70, colorIndex + 20);
 }
//draw graph
 stroke(0);
 fill(255,255,255);
 rect(90,80,100,100);
 for (int index = 0; index<100; index++)
 { 
 if(index == 99)
 tempHistory[index] = tempC;
 else
 tempHistory[index] = tempHistory[index + 1];
 point(90 + index, 180 - tempHistory[index]); 
 }
//write reference values
 fill(10,10,10);
textAlign(RIGHT);
 text("100 C", 45, 25); 
 text("0 C", 45, 187);
//draw triangle pointer
 yDist = int(160 - (160 * (tempC * 0.01)));
 stroke(0);
 triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
//write the temp in C and F
 fill(0,0,0);
textAlign(LEFT);
 text(str((tempC)) + " C", 115, 37);

tempF = ((tempC*9)/5) + 32; text(str((tempF)) + " F", 115, 65); } }

Answers

  • edit post, highlight code, press ctrl-o to format.

    and fix the tags whilst you're there

Sign In or Register to comment.