Hello everybody,
I'm building a food scale with a load cell-HX711 and a 4-digit seven segment display, I'd like to put the device in power down mode when not in use by pressing a button, I wrote the code but something is wrong with the power down mode: in few minutes Arduino wakes up by itself without no external input, I tried to google the forum but nothing helped, what's wrong with the sleep mode?
This is my code
#include "HX711.h"
#include "SevSeg.h"
#include <avr/sleep.h>
SevSeg sevseg;
HX711 scale;
int t, i, n = 20, T;
double val, sum, sumsq, mean;
float stddev;
int sums;// sumsold, sums1, sums2, sums3, sums4, sumr3, sumr4;
int pushtare = A2, pushsleep = 2;
const int interval = 1000;
void Going_To_Sleep(){
//digitalWrite(buzz,HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Sets the sleep mode
cli();
sleep_enable(); // set sleep bit
attachInterrupt(0, wakeUp, LOW); // attach interrupt to wake CPU after sleep.
digitalWrite(LED_BUILTIN, LOW); // turn off LED to show sleep mode
sei();
delay(500);
sleep_cpu(); // put to sleep - will wake up here.
sleep_disable();
detachInterrupt(0); //Removes the interrupt from pin 2;
Serial.println("woke up"); // first line of code executed after sleep.
scale.tare();
digitalWrite(LED_BUILTIN, HIGH);//turning LED on
}
void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor
//detachInterrupt(digitalPinToInterrupt(2)); //Removes the interrupt from pin 2;
// sleep_disable();//Disable sleep mode
//detachInterrupt(0); //Removes the interrupt from pin 2;
}
void setup() {
Serial.begin(9660);
pinMode (pushtare, INPUT);
pinMode (pushsleep, INPUT_PULLUP);
//digitalWrite(pushsleep, HIGH);
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 3, A3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = true;
bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(A1, A0);
Serial.print("Raw ave(20): \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
// Scale factor:
// 1Kg cell: 2020 for reading in gms
// 50kg cells: 19150 for reading in kg
scale.set_scale(421.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("\nAfter setting up the scale:");
Serial.print("Raw: \t\t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("Raw ave(20): \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("Raw ave(5) - tare: \t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("Calibrated ave(5): \t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("\nReadings:");
}
void loop() {
unsigned long now2 = millis(); //prende il tempo
while (millis() < now2 + interval) { //conta fino a del
sevseg.setNumber(sums);
//if (abs(sums - sumsold) > 5){
sevseg.refreshDisplay();
if (digitalRead(pushtare) == HIGH){
scale.tare();
}
if (digitalRead(pushsleep) == HIGH){
Going_To_Sleep();
}
}
//delay(500);
bilancia();
/*
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
*/
}
void bilancia(){
n = 1;
t = millis();
sum = sumsq = 0;
//while (i<n) {
val = ((scale.read() - scale.get_offset()) / scale.get_scale());
sum += val;
sumsq += val * val;
// i++;
//}
t = millis() - t;
mean = sum / n;
//sumsold = sums;
sums = mean;
stddev = sqrt(sumsq / n - mean * mean);
/*Serial.println("sono nel while");
Serial.print("Mean, Std Dev of "); Serial.print(i); Serial.print(" readings:\t");
Serial.print(sum / n, 2); Serial.print("\t"); Serial.print(stddev, 2);
// Note: 2 sigma is 95% confidence, 3 sigma is 99.7%
Serial.print("\nTime taken:\t"); Serial.print(float(t)/1000, 3); Serial.println("Secs\n");
Serial.print("Mean: "); Serial.println(mean);
Serial.print("Sum_int:"); Serial.println(sums);*/
//Divide un numero in cifre singole
/*sums4 = sums%10; //tratta l'unità come resto di una divisione e la mette in una variabile
sumr4 = sums/10; //prende la parte restante del numero
sums3 = sumr4%10; //tratta la come resto di una divisione e la mette in una variabile
sumr3 = sumr4/10; //prende la parte restate del numero
sums2 = sumr3%10; //tratta le centinaia come resto di una divisione e la mette in una variabile
sums1 = sumr3/10; //prende le migliaia come numero
*/
//delay(1000);
}
P.S.: I noticed that if I leave blank the wakeUp() function Arduino wakes immediately after going to sleep, if I put in it the Serial.print message it remains asleep for few minutes, but the RX led remains turned on.