Untitled
c_cpp
a month ago
2.4 kB
2
Indexable
Never
// Define the pin number for the Temp Sensor and LEDs const int TempPin = A0; const int GreenPin = 4; const int YellowPin = 3; const int RedPin = 2; // Reset the state of the Temp Sensor volatile int Temp = 0; // Initialize to 0 // Interrupt Service Routine (ISR) for the Temp Sensor pin ISR(TIMER1_COMPA_vect) { // Read the current state of the button float Temp = analogRead(TempPin); //analog to digital. float Volts=(Temp*5)/1024; //conversion that fits the simulation.(we got values *2 higher than expected) float cel=Volts*100; Serial.print("temp = "); Serial.println(cel); //change the led according to the temp if(cel<30) { // turn on the green LED digitalWrite(GreenPin, HIGH); digitalWrite(YellowPin, LOW); digitalWrite(RedPin, LOW); } //change the led according to the temp if (cel>40&&cel<70) { // turn on the yellow LED digitalWrite(GreenPin, LOW); digitalWrite(YellowPin, HIGH); digitalWrite(RedPin, LOW); } //change the led according to the temp if (cel>70) { // turn on the red LED digitalWrite(GreenPin, LOW); digitalWrite(YellowPin, LOW); digitalWrite(RedPin, HIGH); } } void setup() { // Set the Temp Sensor pin as an input pinMode(TempPin, INPUT); // Set the LEDs pins as an Output pinMode(GreenPin, OUTPUT); pinMode(YellowPin, OUTPUT); pinMode(RedPin, OUTPUT); //int sec; // initialize timer1 noInterrupts(); // disable all interrupts timer1_init(1000);//period time in msec interrupts(); // enable all interrupts // Start communication with the serial monitor at a baud rate of 9600 Serial.begin(9600); } void loop() { // Loop from 0 to 99 and print each number to the serial monitor /* for (int i = 0; i < 100; i++) { Serial.println(i); }*/ } void timer1_init(int msec) { // initialize timer1 to freq=16M/(2*256)=31250Hz //1 sec interrupt--> OCR1A = 31250/2 cycles=15625 cycles=1/2 sec TCCR1A = 0;// set entire TCCR1A register to 0 TCCR1B = 0;// same for TCCR1B TCNT1 = 0;//initialize counter value to 0 // set compare match register for 1hz increments //OCR1A = 15624;// = (1610^6) / (11024) - 1 (must be <65536) OCR1A = (int)15.624*msec; // turn on CTC mode TCCR1B |= (1 << WGM12); // Set CS10 and CS12 bits for 1024 prescaler TCCR1B |= (1 << CS12) | (1 << CS10); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); }