Thursday, 7 March 2013

New Codes

So far, we've wanted the readings to be called rather than continuously send data. Therefore, we changed the the codes into a format where the main PC will be able to call the transmitter to send the data being read.

It turned out as so:


float tempK=0, tempC=0, tempF=0;
void setup(){
  Serial.begin(9600);                    //Setup serial to 9600 bps

}

void loop(){

  Serial.println("Enter '1' to prompt results");

  float reading = analogRead(A0) * 5.0;   //Where 5.0V and the Analog (ADC) input
  reading /= 1024.0;                                // is used to determine the voltage reading

  tempK = reading * 100;                  //Read temperature in Kelvins first

  tempC = tempK - 273.15;                //Convert from Kelvin to Celsius

  tempF = ((tempK) * 9 / 5) - 459.67;    //Convert from Kelvin to Fahrenheit


  while (Serial.available() ==0);       //where no value is entered, no data is sent

  int val = Serial.read() -'0';          //Set the codes to read 'Char' of the ASCII Table
  delay(50);

  if(val == 1)                         //For when the value '1' is given, the data is read
  {
 
    //Print all the values to Serial
    Serial.print("Voltage: ");
    Serial.println(reading);

    Serial.println();

    Serial.print("Kelvin: ");
    Serial.println(tempK);

    Serial.print("Celsius: ");
    Serial.println(tempC);

    Serial.print("Fahrenheit: ");
    Serial.println(tempF);

    Serial.println();    //Print Blank Line
  }
  else                            // For any other data expect for '1', causes an invalid
  {
    Serial.println("Invalid");
  }
  while (Serial.available() > 0) Serial.read();

}

Regarding the ASCII Table, if the code was not placed. The value read from pressing '1' will be '49'
ASCII Table

No comments:

Post a Comment