Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*############################
- ##FISICA APLICADA - PROJETO ##
- ##RAFAEL - 2171639 ##
- ##SAUL - 2171638 ##
- ############################*/
- #include <Servo.h>
- //App running time
- unsigned long timer = 0;
- //EP1
- const int BUZZPIN = 3;
- const int USTRIGGERPIN = 1;
- const int USECHOPIN = 2;
- const int BUZZFREQ = 880;
- const int ALARMSTARTUPTIME = 500;
- const float ALARMDISTANCETRIGGER = 60.0;
- const int ALARMCOUNTDOWN = 10;
- String ALARMCODE = String("123\n");
- short alarmTime;
- bool alarmFired;
- bool alarmDetect;
- //Alarm Update Cycle (seconds)
- unsigned long alarmUpdateCycle;
- //EP2
- const int DOGLEDPIN = 12;
- const int ANALOGIRSENSOR = A1;
- const int IR_TRIGGER = 800;
- int sensorValue = 0;
- float voltage = 0;
- int dogPassTimeOut = 1000;
- int dogNumPasses = 0;
- unsigned long dogPassTime = 0;
- unsigned long dogNextAllowedTime = 0;
- //EP3
- const int AMPOPPIN = A5;
- const float AMPOPGAIN = 1.9;
- const int NTCREDPIN = 5;
- const int NTCYELLOWPIN = 6;
- const int NTCGREENPIN = 7;
- const float NTCBETA = 4090.0;
- const float NTCRZERO = 3300.0;
- const float NTCTZERO = 25.0;
- const int TREF1 = 15;
- const int TREF2 = 28;
- const int TREF3 = 30;
- float ntcCurrentTemp;
- //EP4
- const short LED_NIGHT = 9;
- const short NIGHT_SENSOR = A4;
- const short NIGHT_SENSOR_VALUE = 112;
- const short DAY_SENSOR_VALUE = 1000;
- const short REDUCE_LED_AT_TIME_SECONDS = 10;
- unsigned long REDUCE_CONSUME_TIMER_AUX;
- short TimeToReduce;
- bool reduced;
- int valueLed;
- //EP5
- const int SERVOPIN = 10;
- const int COLDTEMP = 20;
- const int HOTTEMP = 26;
- int servoPos;
- Servo servoMotor;
- void setup()
- {
- Serial.begin(9600);
- //EP1
- pinMode(USTRIGGERPIN,OUTPUT);
- pinMode(USECHOPIN,INPUT);
- //alarm activated time since presence detection
- alarmTime = ALARMCOUNTDOWN;
- //alarm update cycle (every second)
- alarmUpdateCycle = 0;
- alarmFired = false;
- alarmDetect = false;
- //EP2
- pinMode(ANALOGIRSENSOR, INPUT);
- pinMode(DOGLEDPIN, OUTPUT);
- //EP3
- ntcCurrentTemp = 0.0;
- pinMode(NTCREDPIN,OUTPUT);
- pinMode(NTCYELLOWPIN,OUTPUT);
- pinMode(NTCGREENPIN,OUTPUT);
- //EP4
- pinMode(LED_NIGHT, OUTPUT);
- pinMode(NIGHT_SENSOR, INPUT);
- TimeToReduce = REDUCE_LED_AT_TIME_SECONDS;
- reduced = false;
- //EP5
- servoPos = 0;
- servoMotor.attach(SERVOPIN);
- for (servoPos=0; servoPos<=75; servoPos++){
- servoMotor.write(servoPos);
- delay(15);
- }
- InitializeComponents();
- }
- void InitializeComponents(){
- digitalWrite(USTRIGGERPIN, LOW);
- tone(BUZZPIN,BUZZFREQ,ALARMSTARTUPTIME);
- digitalWrite(DOGLEDPIN, HIGH);
- digitalWrite(NTCGREENPIN,HIGH);
- digitalWrite(NTCYELLOWPIN,HIGH);
- digitalWrite(NTCREDPIN,HIGH);
- digitalWrite(LED_NIGHT, HIGH);
- delay(2000);
- digitalWrite(DOGLEDPIN, LOW);
- digitalWrite(NTCGREENPIN,LOW);
- digitalWrite(NTCYELLOWPIN,LOW);
- digitalWrite(NTCREDPIN,LOW);
- digitalWrite(LED_NIGHT, LOW);
- }
- void loop()
- {
- //app running time
- timer = millis();
- //EP1
- //only executes alarm update every second
- if (timer>alarmUpdateCycle){
- alarmUpdateCycle = alarmUpdateCycle + 1000;
- //sending ultrasound trigger (minimum of 10 ms)
- digitalWrite(USTRIGGERPIN, HIGH);
- delayMicroseconds(15);
- digitalWrite(USTRIGGERPIN, LOW);
- //reading ultrasound time and converting to distance in cm
- long duration = pulseIn(USECHOPIN, HIGH);
- float distanceCm = (((duration/1000000.0)*343.0)/2.0)*100.0;
- //read distance if alarm was not fired
- if (!alarmFired){
- if (distanceCm < ALARMDISTANCETRIGGER){
- //shows the warning only at first detection
- if (!alarmDetect){
- Serial.print("Intruso detetado a ");
- Serial.print(distanceCm);
- Serial.println("cm");
- alarmDetect = true;
- }
- Serial.print("O alarme ativa-se em ");
- Serial.print(alarmTime);
- Serial.println(" segundos");
- tone(BUZZPIN,BUZZFREQ,100);
- alarmTime--;
- }else{
- if(alarmDetect){
- Serial.println("Intruso fora da zona de detecao.\nContagem desabilitada!");
- alarmTime = ALARMCOUNTDOWN;
- }
- alarmDetect = false;
- }
- //fired the alarm when countdown ends
- if (alarmTime==0 && !alarmFired){
- alarmFired = true;
- Serial.println("INTRUSAO!");
- tone(BUZZPIN,BUZZFREQ);
- }
- }
- //detecting code input
- if (Serial.available()>0){
- String alarmInput;
- alarmInput = Serial.readString();
- if(alarmInput == ALARMCODE){
- alarmTime = ALARMCOUNTDOWN;
- alarmFired = false;
- alarmDetect = false;
- Serial.println("Alarme desarmado");
- noTone(BUZZPIN);
- }else{
- Serial.println("Codigo errado");
- }
- }
- }
- //EP2
- int irValue = analogRead(ANALOGIRSENSOR);
- if(irValue < IR_TRIGGER){
- dogPassTime = timer; // dog detection time
- //only count one dog movement at time specified time range
- if(dogPassTime > dogNextAllowedTime){
- dogNextAllowedTime = dogPassTime + dogPassTimeOut;
- //count dog movements and turn on the led if there are unpair moves
- if(++dogNumPasses % 2 != 0){
- digitalWrite(DOGLEDPIN, HIGH);
- Serial.println("O snoppy foi fazer xixi ao jardim!");
- }else{
- digitalWrite(DOGLEDPIN, LOW);
- Serial.println("O snoppy esta dentro de casa!");
- }
- }
- }
- //EP3
- int amOpAnalogRead = analogRead(AMPOPPIN);
- float ampOpVolt = amOpAnalogRead * (5.0 / 1023.0);
- float ntcRes = ( (2700.0 * 5.0) / (ampOpVolt/AMPOPGAIN) ) - 2700;
- float ntcReadTemp = ( 1 / ( (1.0/NTCBETA) * log(ntcRes/NTCRZERO) + (1/NTCTZERO) ) ) - 273.15;
- //workaround for simulation. DELETE when in real NTC component
- ntcReadTemp = (ampOpVolt/AMPOPGAIN) / 0.01;
- //END OF TODO
- if ( (ntcReadTemp - ntcCurrentTemp > 0.5) || (ntcReadTemp - ntcCurrentTemp < -0.5) ){
- ntcCurrentTemp = ntcReadTemp;
- Serial.print("Temperatura atual: ");
- Serial.println(ntcCurrentTemp);
- if (ntcCurrentTemp<=TREF1)
- SetTempLeds(true,false,false);
- else if (ntcCurrentTemp>TREF1 && ntcCurrentTemp<=TREF2)
- SetTempLeds(false,false,false);
- else if (ntcCurrentTemp>TREF2 && ntcCurrentTemp<=TREF3)
- SetTempLeds(false,true,false);
- else
- SetTempLeds(false,false,true);
- }
- //EP4
- int night_brightness = analogRead(NIGHT_SENSOR);
- if(millis() > REDUCE_CONSUME_TIMER_AUX && !reduced){
- REDUCE_CONSUME_TIMER_AUX += 1000;
- if(--TimeToReduce == 0){
- reduced = true;
- }
- }
- if(reduced){
- valueLed = map(night_brightness, NIGHT_SENSOR_VALUE, DAY_SENSOR_VALUE, 127, 0);
- }else{
- valueLed = map(night_brightness, NIGHT_SENSOR_VALUE, DAY_SENSOR_VALUE, 255, 0);
- }
- analogWrite(LED_NIGHT, valueLed);
- //Serial.println(night_brightness);
- //EP5
- if (ntcCurrentTemp<COLDTEMP && night_brightness>=DAY_SENSOR_VALUE){
- for (servoPos; servoPos>=30; servoPos--){
- MoveServo(servoPos);
- }
- }else if (ntcCurrentTemp>HOTTEMP && night_brightness>=DAY_SENSOR_VALUE){
- for (servoPos; servoPos<=120; servoPos++){
- MoveServo(servoPos);
- }
- }else{
- if (servoPos>75){
- for (servoPos; servoPos>75; servoPos--){
- MoveServo(servoPos);
- }
- } else if (servoPos<75){
- for (servoPos; servoPos<75; servoPos++){
- MoveServo(servoPos);
- }
- }
- }
- }
- void SetTempLeds (bool greenLed, bool yellowLed, bool redLed){
- if (greenLed)
- digitalWrite(NTCGREENPIN,HIGH);
- else
- digitalWrite(NTCGREENPIN,LOW);
- if (yellowLed)
- digitalWrite(NTCYELLOWPIN,HIGH);
- else
- digitalWrite(NTCYELLOWPIN,LOW);
- if (redLed)
- digitalWrite(NTCREDPIN,HIGH);
- else
- digitalWrite(NTCREDPIN,LOW);
- }
- void MoveServo(int pos){
- servoMotor.write(pos);
- delay(15);
- }
Advertisement
Add Comment
Please, Sign In to add comment