Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TEMP_INTERVAL 1000 //every half a second update the temperature
- #define TEMP1_PIN A0//temperature pin
- #define TEMP2_PIN A1//temperature pin
- #define SWITCH_PIN 7
- #define LED_PIN 13
- const int pins[3] = {9,10,11};//output pins
- const char letters[3] = {'R','G','B'};//letters
- int sensor = TEMP1_PIN;
- unsigned long timeStamp = 0;
- int getTemp();
- void setup() {
- //outputs led
- //analogReference(INTERNAL);//1.1v reference
- for(int i = 0; i < 3; i++)
- {
- pinMode(pins[i],OUTPUT);
- }
- pinMode(LED_PIN,OUTPUT);
- //inputs:
- //pinMode(SWITCH_PIN,INPUT_PULLUP);
- pinMode(TEMP1_PIN,INPUT);
- pinMode(TEMP2_PIN,INPUT);
- Serial.begin(9600);
- timeStamp = millis();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- static boolean state = false;
- if(Serial.available() > 0)
- {
- static char setting;
- static byte data;
- int flag = false;
- setting = Serial.read();//first color is sent, then pwm value as a number
- data = Serial.parseInt();
- data = data & 255;//just ignore all the bits of the bigger number
- //search the right pins for the color
- //if found, raise the flag and send back ack.
- //other wise, send negative ack, probably color mistake
- for(int i = 0; i < 3 && !flag; i++)
- {
- if(setting == letters[i])
- {
- analogWrite(pins[i],data);
- flag = true;
- }
- }
- if(!flag)//not rgb color set
- {
- if(setting == 't')
- {//change the current sensor
- switch(data)
- {
- case 1:
- sensor = TEMP1_PIN;
- digitalWrite(LED_PIN,LOW);
- break;
- case 2:
- digitalWrite(LED_PIN,HIGH);
- sensor = TEMP2_PIN;
- break;
- }
- //Serial.println(sensor);
- }
- }
- delay(20);
- }
- if(millis() - timeStamp >= TEMP_INTERVAL)
- {
- timeStamp = millis();
- //Serial.print("T");
- Serial.print(getTemp(sensor));
- }
- }
- int getTemp(int tempSensor)
- {
- int reading = analogRead(tempSensor);
- //return reading / 9.31;
- return (5 * reading * 100)/1024;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement