#include <HX711.h>
#define dt 2
#define sck 3
HX711 scale; //1 kg = 2.20462262185 lb //mass(lb) = mass(kg) / 0.45359237
//#include <SPI.h>
#include <SD.h>
//variables
File thrustFile;
String fileName = "thrustFile.txt";
int myValue = 100;
const int cs = 10; //sd card pin 10
void setup()
{
{
Serial.begin(9600); //starts the serial monitor at 9600 BAUD
while (!Serial){}; //waits for the serial monitor to start Needed for native USB port boards only:
}
initializeCard()
if(SDFile.exists(thrustFile)) //checks to see if the file is real
{
SDFile.remove(thrustFile); //if the file does exist it is then deleted
Serial.println("\nFile already exists. It has been deleted and will be overwritten\n");
}
writeHeader()
while(myvalue > 0);{ //while our variable is over zero
writeData(); //write another line of data to the file
myValue--; //reduce our cariable by one
}
Serial.println("Data written successfully");
}
void initializeCard()
{
Serial.println("Waiting a little bit..."); //text to serial monitor
delay(1000); //delay 2ms
Serial.print("Getting there..."); //text to serial monitor
delay(1000); //delay 2 ms
Serial.println("We are good to go!!"); //text to serial monitor
delay(1000); //delay 3 milliseconds
Serial.flush(); //not sure of this command
}
Serial.println("Initializing SD card...");
if(!SDFile.begin(cs))
{
Serial.println("Initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (1); // infinite loop forcing to hit reset on uno
}
Serial.println("SD card initialization was successful.");
Serial.println("--------------------------\n");
void writeHeader()
{
thrustFile = SDFile.open(thrustFile, FILE_WRITE); //open the file for editing (write to it)
if(thrustFile){
Serial.println("-----------------------------\n");
Serial.print("Writing to " + thrustFile + ": ");
thrustFile.println("Time,Value");
thrustFile.close();
}
else{
Serial.println("error opening " + thrustFile);
}
}
void writeData(){
thrustFile = SDFile.open(thrustFile, FILE_WRITE);
if(thrustFile)
{
thrustFile.print(millis());
thrustFile.print(",");
thrustFile.println(myValue);
thrustFile.close();
}
else{
Serial.println("error opening " + thrustFile);
}
{
scale.begin(dt, sck); //starts the pins for the scale
Serial.println("Before setting up the scale:"); //text to the serial monitor
Serial.print("read: \t\t"); //prints the word read and provides the variables for the scale data to reside in.
Serial.println(scale.read()); //reads the raw data coming from the scale, ADC to the serial monitor
//delay(2000);
Serial.print("Read average: \t\t"); //prints to serial monitor the words Read Average and then the data below
Serial.println(scale.read_average(20)); //reads the data 20 times from ADC and averages it
//delay(2000); //delay 2ms
Serial.print("Get value: \t\t"); //prints get value to serial monitor
Serial.println(scale.get_value(5)); //reads the data 5 times from ADC minus the tare weight, set with tare() (NOT SET)
//delay(2000);
Serial.print("Get units: \t\t"); //prints to serial monitor
Serial.println(scale.get_units(5),1); //reads the data 5 times from ADC minus the tare weight (not set) divided by the SCALE parameter (NOT SET)
//delay(2000);
}
scale.set_scale(188.f); //this is the value obtained from calibrating with a known weight
scale.tare(); //resets the scale to zero
{
Serial.println("Now the scale is set up:"); //text to the serial monitor
Serial.print("read: \t\t"); //prints the word read and provides the variables for the scale data to reside in.
Serial.println(scale.read()); //reads the raw data coming from the scale, ADC to the serial monitor
//delay(1000);
Serial.print("Read average: \t\t"); //prints to serial monitor the words Read Average and then the data below
Serial.println(scale.read_average(20)); //reads the data 20 times from ADC and averages it
//delay(1000); //delay 2ms
Serial.print("Get value: \t\t"); //prints get value to serial monitor
Serial.println(scale.get_value(5)); //reads the data 5 times from ADC minus the tare weight, set with tare()
//delay(1000);
Serial.print("Get units: \t\t"); //prints to serial monitor
Serial.println(scale.get_units(5),1); //reads the data 5 times from ADC minus the tare weight divided by the SCALE parameter
//delay(1000);
Serial.println("Readings:");
//Serial.print("Value in Lbs. \t\t"); currently disabled!!!!!!!!!!!!!!!!!!!!!!!!!
//Serial.println(scale.read()/.453); currently disabled!!!!!!!!!!!!!!!!!!!!!!!!!
}
void loop()
{
File thrustFile = SDFile.open("thrustFile", FILE_WRITE); // open the file. note that only one file can be open at a time, so you have to close this one before opening another.
if(thrustFile)
{ // if the file is available, write to it:
thrustFile.println("can you hear me?");
thrustFile.close();
//Serial.println(dataString); ??currently disabled!!!!!!!!!!!!!!!!!!!!!!! // print to the serial port too:
}
else
{
Serial.println("error opening thrustFile"); // if the file isn't open, pop up an error:
Serial.print("One reading:\t"); //re-occuring measurement
Serial.print(scale.get_value(5), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(5), 1);
}
//scale.power_down(); currently disabled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//delay(5000); currently disabled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//scale.power_up(); currently disabled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}