بناء نظام إنذار باستخدام اردوينو UNO R3
قصة
في هذا المشروع، سنتعلم كيفية بناء نظام إنذار محلي.
وحدة التحكم Micro Project هذه هي Arduino Uno R3 وسوف نستخدم النقطات في التعليمات البرمجية.
يمكنك إجراء التعديلات الخاصة بك لأن النظام متعدد الاستخدامات.
#define pi 3.14 #define max_lenh 320 /* flags */ bool NORMAL_STATE = false; bool HIGH_STATE = false; bool LOW_STATE = false; bool TEST_MODE = false; bool bip = false; /* time counter */ int timer_cnt = 0; /* photoresistor values */ int light_val = 100; const int max_val = 255; const int min_val = 3; /* pins setup */ const int photoresistor_pin = A0; //photoresistor to arduino pin A0 const int button_pin = 10; //button_pin const int buzzer_pin = 11; //buzzer to arduino pin 11 const int relay_pin = 12; //relay to arduino pin 12 const int led_pin = 13; //led pin void setup() { pinMode(button_pin, INPUT); // Set BUTTON - pin 10 as an input pinMode(buzzer_pin, OUTPUT); // Set buzzer - pin 11 as an output pinMode(relay_pin, OUTPUT); // Set relay - pin 12 as an output pinMode(led_pin, OUTPUT); // Set led - pin 13 as an output Serial.begin(9600); // TIMER 1 for interrupt frequency 2 Hz: cli(); // stop interrupts 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 2 Hz increments OCR1A = 31249; // = 16000000 / (256 * 2) - 1 (must be <65536) // turn on CTC mode TCCR1B |= (1 << WGM12); // Set CS12, CS11 and CS10 bits for 256 prescaler TCCR1B |= (1 << CS12) | (0 << CS11) | (0 << CS10); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); sei(); // allow interrupts } void loop() { } ISR(TIMER1_COMPA_vect){ if(timer_cnt == 0) //cheak value of photoresistor when counter = 0 { light_val = analogRead(photoresistor_pin); if(light_val < min_val) { NORMAL_STATE = false; HIGH_STATE = false; LOW_STATE = true; TEST_MODE = false; } else if(light_val > max_val) { NORMAL_STATE = false; HIGH_STATE = true; LOW_STATE = false; TEST_MODE = false; } else { NORMAL_STATE = true; HIGH_STATE = false; LOW_STATE = false; TEST_MODE = false; } } if(digitalRead(button_pin)) //test mode { NORMAL_STATE = false; HIGH_STATE = false; LOW_STATE = false; TEST_MODE = true; } if(LOW_STATE) { if(bip) { tone(buzzer_pin, 500); // Send 0.5KHz sound signal... bip = false; } else { noTone(buzzer_pin); // Stop sound... bip = true; } digitalWrite(relay_pin, LOW); //turns off light bulb digitalWrite(led_pin, LOW); //turns off led } else if(HIGH_STATE) { noTone(buzzer_pin); bip = false; digitalWrite(relay_pin, HIGH); //turns ON light bulb digitalWrite(led_pin, LOW); //turns off led } else if(NORMAL_STATE) { noTone(buzzer_pin); bip = false; digitalWrite(relay_pin, LOW); //turns off light bulb digitalWrite(led_pin, HIGH); //turns on led } else if(TEST_MODE) { noTone(buzzer_pin); bip = false; digitalWrite(relay_pin, HIGH); //turns on light bulb digitalWrite(led_pin, HIGH); //turns on led }
ليست هناك تعليقات:
إرسال تعليق