Hello,
I have a weight sensor with an external power. I used an arduino pro micro. When i print values on the serial monitor and SD card, everything is good but when i disconnect the usb and continue to print the data on the SD card, there is some issues. I get strange values. Did you know whats happening ?
My code :
#include "Arduino.h"
#include <HX711.h>
#include <SPI.h>
#include <SD.h>
#include <LowPower.h>
//void calibrate();
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 7;
File myFile;
HX711 scale;
void setup() {
Serial.begin(57600);
while (!Serial);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
SD.begin(4);
myFile = SD.open("res.txt", FILE_WRITE);
if (myFile) {
Serial.println(F("File opened ok"));
myFile.println("weight");
myFile.close();
//
} else {
// if the file didn't open, print an error:
Serial.println(F("error opening sd file"));
}
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_gain(128);
calibrate();
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\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("get units: \t\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("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
myFile = SD.open("res.txt", FILE_WRITE);
if (myFile) {
myFile.print(scale.get_units(10),1);
myFile.println(",");
}
myFile.close();
scale.power_down();
lowPowerSleep(1);
// put the ADC in sleep mode
//delay(2000);
//delay(60000);
scale.power_up();
}
void calibrate() {
// Remove any calibration values and clear the scale
scale.set_scale();
scale.tare();
// Prompt the user
Serial.println("Add your known weight to the scale, enter the weight and press <Enter>");
int userInput = -123;
String inputString = "";
// Loop until we receive an input (which will change the value of userInput
while (userInput == -123) {
// Read serial input:
while (Serial.available() > 0) {
int inputChar = Serial.read();
Serial.print("inputChar:");
Serial.println(inputChar);
if (isDigit(inputChar)) {
// convert the incoming byte to a char and add it to the string:
inputString += (char)inputChar;
Serial.print("inputString:");
Serial.println(inputString);
//userInput = inputString.toInt();
userInput = inputString.toInt();
Serial.print("userInput");
Serial.println(userInput);
}
// if you get a newline, print the string, then the string's value:
// if (inputChar == '\n') {
// userInput = inputString.toInt();
// Serial.print("userInput");
// Serial.println(userInput);
// }
}
}
// Now get the reading from the scale
float calReading = scale.get_units(10);
Serial.print("Setting the cal to ");
Serial.println(calReading / userInput);
scale.set_scale(calReading / userInput);
}
void lowPowerSleep(int minutes)
{
int seconds = minutes * 60;
int sleeps = seconds / 8;
for (int i = 0 ; i < sleeps ; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
The SD card values :
2544.3,
2531.3,
2528.9,
2514.9,
disconect usb ,
476.4,186.7,
-270.0,
225.4,
-418.8,169.4,
-1071.8,108.1,
-1097.4,108.9,