I have been working with a load cell to record weight. I am wondering if there is a way to have the scale take a few readings and then average. Right now the scale is not working well enough to get any useful readings out of it. I am also working on the watchdog timer code because I am not sure that the arduino is actually sleeping. It still has LEDS on when it is supposed to be shutdown so I am not confident that its asleep. Here is the code I have for the scale.
float aReading = 20;
float aLoad = 0; // lbs.
float bReading = 161;
float bLoad = 215; // lbs.
long time = 0;
//int interval = 10000; // Take a reading every 500 ms
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SPI.h>
#include <SD.h>
#include <Sleep_n0m1.h>
Sleep sleep;
unsigned long sleepTime; //how long you want the arduino to sleep
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
//int buzzer = 11;
int led = 8;
int scalepower = 6; // try to power scale intermitantly??
void setup()
{
pinMode(led, OUTPUT);
pinMode(scalepower, OUTPUT);
//digitalWrite(led, HIGH);
Serial.begin(9600);
sleepTime = 3600000; //set sleep time in ms, max sleep time is 49.7 days
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
//Serial.println("card initialized.");
}
void loop()
{
//digitalWrite(buzzer, HIGH);
//delay(200);
//digitalWrite(buzzer, LOW);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(6000);
digitalWrite(led, LOW);
digitalWrite(scalepower, HIGH);
delay(10000);
float newReading = analogRead(2);
//calculate load based on A and B Readings above
float load = ((bLoad - aLoad)/(bReading - aReading)) * (newReading - aReading) + aLoad;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(newReading,2);
dataFile.print(",");
dataFile.println(load,2);
dataFile.close();
//print to the serial port too:
Serial.print(newReading,2);
Serial.print(",");
Serial.println(load,2);
delay(3000);
digitalWrite(scalepower, LOW);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(3000);
digitalWrite(led, LOW);
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}