Guest User

Problems with Serial Reading

a guest
Sep 17th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
  4.  
  5. int buttState;
  6.  
  7. int buttNum = 0;
  8.  
  9. int greenPin = 11;
  10. int redPin = 12;
  11. int bluePin = 13;
  12. int buttPin = 2;
  13. int tempPin = 0;
  14.  
  15. float tempC;
  16. int tempF;
  17. float reading;
  18. float voltage;
  19.  
  20. char firstLine[16]; //Potential problem area
  21. char secondLine[16];
  22.  
  23. void setup()
  24. {
  25.   Serial.begin(9600);
  26.   lcd.begin(16, 2);
  27.  
  28.   pinMode(greenPin, OUTPUT);
  29.   pinMode(redPin, OUTPUT);
  30.   pinMode(bluePin, OUTPUT);
  31.   pinMode(buttPin, INPUT);
  32.   pinMode(tempPin, INPUT);
  33.  
  34.   Serial.println("1st Line: ");  //Potential problem area
  35.   while (Serial.available() == 0);
  36.   firstLine[16] = Serial.read();
  37.   Serial.println(firstLine);
  38.   lcd.setCursor(0, 0);
  39.   lcd.print(firstLine);
  40.    
  41.   Serial.println("2nd Line: ");  //Potential problem area
  42.   secondLine[16] = Serial.read();
  43.   while (Serial.available() == 0);
  44.   Serial.println(secondLine);
  45.   lcd.setCursor(0, 1);
  46.   lcd.print(secondLine);
  47. }
  48.  
  49. int readTemp()
  50. {
  51.   reading = analogRead(tempPin);
  52.   voltage = reading * 5.0;
  53.   voltage /= 1024.0;
  54.   tempC = (voltage - 0.5) * 100 ;
  55.   tempF = (tempC * 9.0 / 5.0) + 32.0;
  56.   Serial.print(tempF); Serial.println(" degrees F");
  57.   lcd.setCursor(13, 0);
  58.   lcd.print(tempF);
  59.   lcd.print("F");
  60. }
  61. void loop()
  62. {
  63.   buttState = digitalRead(buttPin);
  64.  
  65.   readTemp();
  66.  
  67.   if (buttState == LOW)
  68.   {
  69.     buttNum++;
  70.   }
  71.  
  72.   if (buttNum > 3)
  73.   {
  74.     buttNum = 0;
  75.   }
  76.  
  77.   if (buttNum == 1)
  78.   {
  79.     digitalWrite(greenPin, LOW);
  80.   }
  81.   else
  82.   {
  83.     digitalWrite(greenPin, HIGH);
  84.   }
  85.  
  86.   if (buttNum == 2)
  87.   {
  88.     digitalWrite(redPin, LOW);
  89.   }
  90.   else
  91.   {
  92.     digitalWrite(redPin, HIGH);
  93.   }
  94.  
  95.   if (buttNum == 3)
  96.   {
  97.     digitalWrite(bluePin, LOW);
  98.   }
  99.   else
  100.   {
  101.     digitalWrite(bluePin, HIGH);
  102.   }
  103.  
  104.   Serial.println(firstLine);
  105.   Serial.println(secondLine);
  106.  
  107.   delay(2000);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment