Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ReadAnalogVoltage
- Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
- Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
- Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
- This example code is in the public domain.
- */
- // the setup routine runs once when you press reset:
- void setup() {
- // initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- pinMode(LED_BUILTIN, OUTPUT);
- }
- float average = 0.0;
- float threshold = 0.004;
- int loops_to_blink=0;
- int currentState=0; //ar lemputė įjungta ar išjungta
- float diff=0.0;
- // the loop routine runs over and over again forever:
- void loop() {
- // read the input on analog pin 0:
- int sensorValue = analogRead(A0);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3,3V):
- float voltage = sensorValue * (3.3 / 1023.0);
- // Perskaičiuojame vidurkį po kiekvieno matavimo
- average = 0.98*average+0.02*voltage;
- // skirtumas
- diff = voltage-average;
- if (abs(diff)>threshold) {
- loops_to_blink=200;
- Serial.print("voltage:");
- Serial.println(voltage);
- Serial.print("diff*100:");
- Serial.println(diff*100);
- Serial.println("");
- }
- if (loops_to_blink>1) {
- loops_to_blink--;
- // šitas kodas parašytas, kad įjungti lemputė tik kai jį buvo išjungta
- // EFEKTYVUMO nekeičia
- if (currentState==0) {
- digitalWrite(LED_BUILTIN, HIGH);
- }
- currentState=1;
- }else{
- // šitas kodas parašytas, kad išjungti lemputė tik kai jį buvo įjungta
- // EFEKTYVUMO nekeičia
- if (currentState==1) {
- digitalWrite(LED_BUILTIN, LOW);
- }
- currentState=0;
- }
- // delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement