#include /* Software serial multple serial test Receives from the hardware serial, sends to software serial. Receives from software serial, sends to hardware serial. The circuit: * RX is digital pin 2 (connect to TX of other device) * TX is digital pin 3 (connect to RX of other device) created back in the mists of time modified 9 Apr 2012 by Tom Igoe based on Mikal Hart's example This example code is in the public domain. */ SoftwareSerial mySerial(0, 1); // TX, RX //int counter; uint8_t blue_led = 2, green_led = 3, red_led = 7; int temp_offset = 280, temperature;//, min_temp = 20, max_temp = 30, pwm; int us_delay; void setup() { // set the data rate for the SoftwareSerial port mySerial.begin(9600); ADCSRA |= 0b10000000; //delay(1); // << this makes ADC saturate at 1023 pinMode(red_led, OUTPUT); pinMode(green_led, OUTPUT); pinMode(blue_led, OUTPUT); } void loop() // run over and over { temperature = readTemp(); mySerial.print(millis()); mySerial.print(","); //mySerial.print("temp: "); mySerial.print(temperature); mySerial.print(","); //mySerial.print(" delay: "); mySerial.println(us_delay); us_delay = (temperature-20)*127/(120-28)+us_delay/2; if (us_delay > 255) us_delay = 255; else if (us_delay < 0) us_delay = 0; analogWrite(red_led, 255-us_delay); analogWrite(green_led, 255); analogWrite(blue_led, 255); } int readTemp() { // Read temperature sensor against 1.1V reference ADMUX = _BV(REFS1) | _BV(MUX5) | _BV(MUX1); delay(2); // Wait for ADMUX setting to settle ADCSRA |= _BV(ADSC); // Start conversion while (bit_is_set(ADCSRA,ADSC)); // measuring uint8_t low = ADCL; // must read ADCL first - it then locks ADCH uint8_t high = ADCH; // unlocks both int result = (high << 8) | low; // combine the two return result-temp_offset; }