Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. /*
  2. ReadAnalogVoltage
  3. Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  4. Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  5. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
  6.  
  7. This example code is in the public domain.
  8. */
  9.  
  10. // the setup routine runs once when you press reset:
  11. void setup() {
  12. // initialize serial communication at 9600 bits per second:
  13. Serial.begin(9600);
  14. pinMode(LED_BUILTIN, OUTPUT);
  15. }
  16.  
  17.  
  18. float average = 0.0;
  19. float threshold = 0.004;
  20. int loops_to_blink=0;
  21. int currentState=0; //ar lemputė įjungta ar išjungta
  22. float diff=0.0;
  23. // the loop routine runs over and over again forever:
  24. void loop() {
  25. // read the input on analog pin 0:
  26. int sensorValue = analogRead(A0);
  27. // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3,3V):
  28. float voltage = sensorValue * (3.3 / 1023.0);
  29. // Perskaičiuojame vidurkį po kiekvieno matavimo
  30. average = 0.98*average+0.02*voltage;
  31. // skirtumas
  32. diff = voltage-average;
  33. if (abs(diff)>threshold) {
  34. loops_to_blink=200;
  35. Serial.print("voltage:");
  36. Serial.println(voltage);
  37. Serial.print("diff*100:");
  38. Serial.println(diff*100);
  39. Serial.println("");
  40. }
  41. if (loops_to_blink>1) {
  42. loops_to_blink--;
  43. // šitas kodas parašytas, kad įjungti lemputė tik kai jį buvo išjungta
  44. // EFEKTYVUMO nekeičia
  45. if (currentState==0) {
  46. digitalWrite(LED_BUILTIN, HIGH);
  47. }
  48. currentState=1;
  49. }else{
  50. // šitas kodas parašytas, kad išjungti lemputė tik kai jį buvo įjungta
  51. // EFEKTYVUMO nekeičia
  52. if (currentState==1) {
  53. digitalWrite(LED_BUILTIN, LOW);
  54. }
  55. currentState=0;
  56. }
  57. // delay(10);
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement