Guest User

Untitled

a guest
Jan 10th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. /*############################
  2. ##FISICA APLICADA - PROJETO ##
  3. ##RAFAEL - 2171639 ##
  4. ##SAUL - 2171638 ##
  5. ############################*/
  6.  
  7. #include <Servo.h>
  8.  
  9. //App running time
  10. unsigned long timer = 0;
  11.  
  12.  
  13. //EP1
  14. const int BUZZPIN = 3;
  15. const int USTRIGGERPIN = 1;
  16. const int USECHOPIN = 2;
  17. const int BUZZFREQ = 880;
  18. const int ALARMSTARTUPTIME = 500;
  19. const float ALARMDISTANCETRIGGER = 60.0;
  20. const int ALARMCOUNTDOWN = 10;
  21. String ALARMCODE = String("123\n");
  22. short alarmTime;
  23. bool alarmFired;
  24. bool alarmDetect;
  25. //Alarm Update Cycle (seconds)
  26. unsigned long alarmUpdateCycle;
  27.  
  28.  
  29. //EP2
  30. const int DOGLEDPIN = 12;
  31. const int ANALOGIRSENSOR = A1;
  32. const int IR_TRIGGER = 800;
  33. int sensorValue = 0;
  34. float voltage = 0;
  35. int dogPassTimeOut = 1000;
  36. int dogNumPasses = 0;
  37. unsigned long dogPassTime = 0;
  38. unsigned long dogNextAllowedTime = 0;
  39.  
  40.  
  41. //EP3
  42. const int AMPOPPIN = A5;
  43. const float AMPOPGAIN = 1.9;
  44. const int NTCREDPIN = 5;
  45. const int NTCYELLOWPIN = 6;
  46. const int NTCGREENPIN = 7;
  47. const float NTCBETA = 4090.0;
  48. const float NTCRZERO = 3300.0;
  49. const float NTCTZERO = 25.0;
  50. const int TREF1 = 15;
  51. const int TREF2 = 28;
  52. const int TREF3 = 30;
  53. float ntcCurrentTemp;
  54.  
  55.  
  56. //EP4
  57. const short LED_NIGHT = 9;
  58. const short NIGHT_SENSOR = A4;
  59. const short NIGHT_SENSOR_VALUE = 112;
  60. const short DAY_SENSOR_VALUE = 1000;
  61. const short REDUCE_LED_AT_TIME_SECONDS = 10;
  62. unsigned long REDUCE_CONSUME_TIMER_AUX;
  63. short TimeToReduce;
  64. bool reduced;
  65. int valueLed;
  66.  
  67.  
  68. //EP5
  69. const int SERVOPIN = 10;
  70. const int COLDTEMP = 20;
  71. const int HOTTEMP = 26;
  72. int servoPos;
  73. Servo servoMotor;
  74.  
  75.  
  76. void setup()
  77. {
  78. Serial.begin(9600);
  79.  
  80.  
  81. //EP1
  82. pinMode(USTRIGGERPIN,OUTPUT);
  83. pinMode(USECHOPIN,INPUT);
  84. //alarm activated time since presence detection
  85. alarmTime = ALARMCOUNTDOWN;
  86. //alarm update cycle (every second)
  87. alarmUpdateCycle = 0;
  88. alarmFired = false;
  89. alarmDetect = false;
  90.  
  91.  
  92. //EP2
  93. pinMode(ANALOGIRSENSOR, INPUT);
  94. pinMode(DOGLEDPIN, OUTPUT);
  95.  
  96.  
  97. //EP3
  98. ntcCurrentTemp = 0.0;
  99. pinMode(NTCREDPIN,OUTPUT);
  100. pinMode(NTCYELLOWPIN,OUTPUT);
  101. pinMode(NTCGREENPIN,OUTPUT);
  102.  
  103.  
  104. //EP4
  105. pinMode(LED_NIGHT, OUTPUT);
  106. pinMode(NIGHT_SENSOR, INPUT);
  107. TimeToReduce = REDUCE_LED_AT_TIME_SECONDS;
  108. reduced = false;
  109.  
  110.  
  111.  
  112. //EP5
  113. servoPos = 0;
  114. servoMotor.attach(SERVOPIN);
  115. for (servoPos=0; servoPos<=75; servoPos++){
  116. servoMotor.write(servoPos);
  117. delay(15);
  118. }
  119.  
  120.  
  121. InitializeComponents();
  122. }
  123.  
  124. void InitializeComponents(){
  125. digitalWrite(USTRIGGERPIN, LOW);
  126. tone(BUZZPIN,BUZZFREQ,ALARMSTARTUPTIME);
  127. digitalWrite(DOGLEDPIN, HIGH);
  128. digitalWrite(NTCGREENPIN,HIGH);
  129. digitalWrite(NTCYELLOWPIN,HIGH);
  130. digitalWrite(NTCREDPIN,HIGH);
  131. digitalWrite(LED_NIGHT, HIGH);
  132. delay(2000);
  133. digitalWrite(DOGLEDPIN, LOW);
  134. digitalWrite(NTCGREENPIN,LOW);
  135. digitalWrite(NTCYELLOWPIN,LOW);
  136. digitalWrite(NTCREDPIN,LOW);
  137. digitalWrite(LED_NIGHT, LOW);
  138. }
  139.  
  140. void loop()
  141. {
  142. //app running time
  143. timer = millis();
  144.  
  145.  
  146. //EP1
  147. //only executes alarm update every second
  148. if (timer>alarmUpdateCycle){
  149. alarmUpdateCycle = alarmUpdateCycle + 1000;
  150.  
  151. //sending ultrasound trigger (minimum of 10 ms)
  152. digitalWrite(USTRIGGERPIN, HIGH);
  153. delayMicroseconds(15);
  154. digitalWrite(USTRIGGERPIN, LOW);
  155.  
  156. //reading ultrasound time and converting to distance in cm
  157. long duration = pulseIn(USECHOPIN, HIGH);
  158. float distanceCm = (((duration/1000000.0)*343.0)/2.0)*100.0;
  159.  
  160.  
  161. //read distance if alarm was not fired
  162. if (!alarmFired){
  163. if (distanceCm < ALARMDISTANCETRIGGER){
  164. //shows the warning only at first detection
  165. if (!alarmDetect){
  166. Serial.print("Intruso detetado a ");
  167. Serial.print(distanceCm);
  168. Serial.println("cm");
  169. alarmDetect = true;
  170. }
  171.  
  172. Serial.print("O alarme ativa-se em ");
  173. Serial.print(alarmTime);
  174. Serial.println(" segundos");
  175.  
  176. tone(BUZZPIN,BUZZFREQ,100);
  177. alarmTime--;
  178.  
  179. }else{
  180. if(alarmDetect){
  181. Serial.println("Intruso fora da zona de detecao.\nContagem desabilitada!");
  182. alarmTime = ALARMCOUNTDOWN;
  183. }
  184. alarmDetect = false;
  185. }
  186.  
  187. //fired the alarm when countdown ends
  188. if (alarmTime==0 && !alarmFired){
  189. alarmFired = true;
  190. Serial.println("INTRUSAO!");
  191. tone(BUZZPIN,BUZZFREQ);
  192. }
  193. }
  194.  
  195. //detecting code input
  196. if (Serial.available()>0){
  197. String alarmInput;
  198. alarmInput = Serial.readString();
  199. if(alarmInput == ALARMCODE){
  200. alarmTime = ALARMCOUNTDOWN;
  201. alarmFired = false;
  202. alarmDetect = false;
  203. Serial.println("Alarme desarmado");
  204. noTone(BUZZPIN);
  205. }else{
  206. Serial.println("Codigo errado");
  207. }
  208. }
  209.  
  210. }
  211.  
  212.  
  213. //EP2
  214. int irValue = analogRead(ANALOGIRSENSOR);
  215. if(irValue < IR_TRIGGER){
  216. dogPassTime = timer; // dog detection time
  217. //only count one dog movement at time specified time range
  218. if(dogPassTime > dogNextAllowedTime){
  219. dogNextAllowedTime = dogPassTime + dogPassTimeOut;
  220. //count dog movements and turn on the led if there are unpair moves
  221. if(++dogNumPasses % 2 != 0){
  222. digitalWrite(DOGLEDPIN, HIGH);
  223. Serial.println("O snoppy foi fazer xixi ao jardim!");
  224. }else{
  225. digitalWrite(DOGLEDPIN, LOW);
  226. Serial.println("O snoppy esta dentro de casa!");
  227. }
  228. }
  229. }
  230.  
  231.  
  232. //EP3
  233. int amOpAnalogRead = analogRead(AMPOPPIN);
  234. float ampOpVolt = amOpAnalogRead * (5.0 / 1023.0);
  235.  
  236. float ntcRes = ( (2700.0 * 5.0) / (ampOpVolt/AMPOPGAIN) ) - 2700;
  237. float ntcReadTemp = ( 1 / ( (1.0/NTCBETA) * log(ntcRes/NTCRZERO) + (1/NTCTZERO) ) ) - 273.15;
  238.  
  239. //workaround for simulation. DELETE when in real NTC component
  240. ntcReadTemp = (ampOpVolt/AMPOPGAIN) / 0.01;
  241. //END OF TODO
  242.  
  243. if ( (ntcReadTemp - ntcCurrentTemp > 0.5) || (ntcReadTemp - ntcCurrentTemp < -0.5) ){
  244. ntcCurrentTemp = ntcReadTemp;
  245. Serial.print("Temperatura atual: ");
  246. Serial.println(ntcCurrentTemp);
  247.  
  248. if (ntcCurrentTemp<=TREF1)
  249. SetTempLeds(true,false,false);
  250. else if (ntcCurrentTemp>TREF1 && ntcCurrentTemp<=TREF2)
  251. SetTempLeds(false,false,false);
  252. else if (ntcCurrentTemp>TREF2 && ntcCurrentTemp<=TREF3)
  253. SetTempLeds(false,true,false);
  254. else
  255. SetTempLeds(false,false,true);
  256.  
  257. }
  258.  
  259.  
  260. //EP4
  261. int night_brightness = analogRead(NIGHT_SENSOR);
  262.  
  263. if(millis() > REDUCE_CONSUME_TIMER_AUX && !reduced){
  264. REDUCE_CONSUME_TIMER_AUX += 1000;
  265.  
  266. if(--TimeToReduce == 0){
  267. reduced = true;
  268. }
  269. }
  270.  
  271. if(reduced){
  272. valueLed = map(night_brightness, NIGHT_SENSOR_VALUE, DAY_SENSOR_VALUE, 127, 0);
  273. }else{
  274. valueLed = map(night_brightness, NIGHT_SENSOR_VALUE, DAY_SENSOR_VALUE, 255, 0);
  275. }
  276.  
  277. analogWrite(LED_NIGHT, valueLed);
  278. //Serial.println(night_brightness);
  279.  
  280.  
  281. //EP5
  282. if (ntcCurrentTemp<COLDTEMP && night_brightness>=DAY_SENSOR_VALUE){
  283. for (servoPos; servoPos>=30; servoPos--){
  284. MoveServo(servoPos);
  285. }
  286. }else if (ntcCurrentTemp>HOTTEMP && night_brightness>=DAY_SENSOR_VALUE){
  287. for (servoPos; servoPos<=120; servoPos++){
  288. MoveServo(servoPos);
  289. }
  290. }else{
  291. if (servoPos>75){
  292. for (servoPos; servoPos>75; servoPos--){
  293. MoveServo(servoPos);
  294. }
  295. } else if (servoPos<75){
  296. for (servoPos; servoPos<75; servoPos++){
  297. MoveServo(servoPos);
  298. }
  299. }
  300. }
  301. }
  302.  
  303. void SetTempLeds (bool greenLed, bool yellowLed, bool redLed){
  304. if (greenLed)
  305. digitalWrite(NTCGREENPIN,HIGH);
  306. else
  307. digitalWrite(NTCGREENPIN,LOW);
  308.  
  309. if (yellowLed)
  310. digitalWrite(NTCYELLOWPIN,HIGH);
  311. else
  312. digitalWrite(NTCYELLOWPIN,LOW);
  313.  
  314. if (redLed)
  315. digitalWrite(NTCREDPIN,HIGH);
  316. else
  317. digitalWrite(NTCREDPIN,LOW);
  318. }
  319.  
  320. void MoveServo(int pos){
  321. servoMotor.write(pos);
  322. delay(15);
  323. }
Advertisement
Add Comment
Please, Sign In to add comment