diff --git a/measureMass/measureMass.ino b/measureMass/measureMass.ino index 5cd34b6..383b054 100644 --- a/measureMass/measureMass.ino +++ b/measureMass/measureMass.ino @@ -1,7 +1,12 @@ #include +int const Fs = 200; +int const timePeriod = 1000/Fs; //for delay +float timeStart = 0; + int const myScale = A0; -int unsigned avgValue = 0; +long unsigned avgValue = 0; +long unsigned lastVal = 0; int const movAvgVal = 20; Servo myservo; @@ -16,15 +21,27 @@ void setup() { void loop() { // put your main code here, to run repeatedly: - int localValue = 0; + long unsigned localValue = 0; + for (int i = 0; i < movAvgVal; i++){ + timeStart = micros(); localValue = localValue + analogRead(myScale); + int readTime = timeStart - micros(); + delay(timePeriod-readTime/1000); } + avgValue = localValue/movAvgVal; - Serial.println(avgValue); + int compare = avgValue - lastVal; - val = map(avgValue, 80, 900, 0, 180); - myservo.write(val); - delay(15); + if (compare > 1 || compare < -1){ + val = map(avgValue, 345, 870, 0, 180); + myservo.write(val); + Serial.print("current VAL: "); + Serial.println(avgValue); + Serial.print("compare VAL: "); + Serial.println(compare); + delay(15); + lastVal = avgValue; + } }