Advertisement
Noam_Wiseman

Arduino file humming board server

Apr 1st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #define TEMP_INTERVAL 1000 //every half a second update the temperature
  2.  
  3. #define TEMP1_PIN A0//temperature pin
  4. #define TEMP2_PIN A1//temperature pin
  5.  
  6. #define SWITCH_PIN 7
  7. #define LED_PIN 13
  8. const int pins[3] = {9,10,11};//output pins
  9. const char letters[3] = {'R','G','B'};//letters
  10. int sensor = TEMP1_PIN;
  11.  
  12. unsigned long timeStamp = 0;
  13. int getTemp();
  14. void setup() {
  15.   //outputs led
  16.   //analogReference(INTERNAL);//1.1v reference
  17.   for(int i = 0; i < 3; i++)
  18.   {
  19.     pinMode(pins[i],OUTPUT);
  20.   }
  21.   pinMode(LED_PIN,OUTPUT);
  22.   //inputs:
  23.   //pinMode(SWITCH_PIN,INPUT_PULLUP);
  24.   pinMode(TEMP1_PIN,INPUT);
  25.   pinMode(TEMP2_PIN,INPUT);
  26.   Serial.begin(9600);
  27.   timeStamp = millis();
  28. }
  29.  
  30. void loop() {
  31.   // put your main code here, to run repeatedly:
  32.   static boolean state = false;
  33.  
  34.   if(Serial.available() > 0)
  35.   {
  36.     static char setting;
  37.     static byte data;
  38.     int flag = false;
  39.     setting = Serial.read();//first color is sent, then pwm value as a number
  40.    
  41.     data = Serial.parseInt();
  42.     data = data & 255;//just ignore all the bits of the bigger number
  43.     //search the right pins for the color
  44.     //if found, raise the flag and send back ack.
  45.     //other wise, send negative ack, probably color mistake
  46.     for(int i = 0; i < 3 && !flag; i++)
  47.     {
  48.       if(setting == letters[i])
  49.       {
  50.         analogWrite(pins[i],data);
  51.         flag = true;
  52.       }
  53.     }
  54.     if(!flag)//not rgb color set
  55.     {
  56.       if(setting == 't')
  57.       {//change the current sensor
  58.         switch(data)
  59.         {
  60.           case 1:
  61.             sensor = TEMP1_PIN;
  62.             digitalWrite(LED_PIN,LOW);
  63.             break;
  64.           case 2:
  65.             digitalWrite(LED_PIN,HIGH);
  66.             sensor = TEMP2_PIN;
  67.             break;
  68.         }
  69.         //Serial.println(sensor);
  70.       }
  71.     }
  72.     delay(20);
  73.    
  74.   }
  75.   if(millis() - timeStamp >= TEMP_INTERVAL)
  76.   {
  77.     timeStamp = millis();
  78.     //Serial.print("T");
  79.     Serial.print(getTemp(sensor));
  80.   }
  81. }
  82.  
  83.  
  84. int getTemp(int tempSensor)
  85. {
  86.   int reading = analogRead(tempSensor);
  87.   //return reading / 9.31;
  88.   return (5 * reading * 100)/1024;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement