SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By mahardie
#201138
hi, i need to add a switch to logging scales to shift logging intervals...help

//This is a bisic working version
//still have to connect red wire to 5vt
//problem LCD dimming
//error led is working only staying on if fail, no flash-great
// linked syn time and delay to log interval
//short flash with longer log intervall

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "HX711.h"
HX711 scale(3, 2);
#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = A0, d5 = A1, d6 = A2, d7 = A3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // 10000 = 5 mins, 5000 = 1 min, 1000 = xx mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!

#define SYNC_INTERVAL LOG_INTERVAL //

uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
#define redLEDpin 6
#define greenLEDpin 7


RTC_PCF8523 rtc;//RTC_DS1307 RTC; // define the Real Time Clock object

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{ Serial.print("error: ");
Serial.println(str);
digitalWrite(redLEDpin, HIGH); // red LED indicates error
while(1); }
//HX711 scale(3, 2); // parameter "gain" is ommited; the default value 128 is used by the library

void setup(void)
{ Serial.begin(9600);
Serial.println();
pinMode(redLEDpin, OUTPUT); // use debugging LEDs
pinMode(greenLEDpin, OUTPUT);
{ lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.print("Zeroing Scales....");// Print a message to the LCD.
}
Serial.print("Initializing SD card...");// initialize the SD card
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) // see if the card is present and can be initialized:
error("Card failed, or not present");
Serial.println("card initialized.");
char filename[] = "LOGGER00.CSV"; // create a new file
for (uint8_t i = 0; i < 100; i++)
{
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename))
{ logfile = SD.open(filename, FILE_WRITE); // only open a new file if it doesn't exist
break; // leave the loop!
}
}
if (! logfile)
{ error("couldnt create file");
}
Serial.print("Logging to: "); // have to fix this
Serial.println(filename);// have to fix this
Wire.begin(); // connect to RTC
if (! rtc.begin())
{ Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized())
{ Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));// following line sets the RTC to the date & time this sketch was compiled
}
logfile.println("Elapsed Mins,Date,Time (h:m:s),Day,Instant,Average");
#if ECHO_TO_SERIAL
#endif //ECHO_TO_SERIAL
analogReference(EXTERNAL); // If you want to set the aref to something other than 5v
{ Serial.println("HX711 Demo");
Serial.println("Before setting up the scale:");
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 (not set yet)
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 (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(2280.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("After setting up the scale:");
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
Serial.println("");// by the SCALE parameter set with set_scale
Serial.println("OMG its about to start............................");
Serial.println("");
Serial.println("Elapsed mins,Date,Time,Instant,Average");
}

}

void loop(void)
{ lcd.begin(16, 2);
lcd.print("Logging..."); // Print a message to the LCD.
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("LoadCell: "); // Prints Sensor Val: to LCD
lcd.print(scale.get_units());


{ delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));// delay for the amount of time we want between readings
uint32_t m = millis(); // log milliseconds since starting
#if ECHO_TO_SERIAL
#endif}
DateTime now = rtc.now();
logfile.print(m/60000); // milliseconds since start
logfile.print(", ");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(", ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(", ");
logfile.print(daysOfTheWeek[now.dayOfTheWeek()]);
logfile.print(", "); ///
logfile.print("\t");//
logfile.print(scale.get_units(), 2);//
logfile.print(", ");
logfile.print("\t");//
logfile.print(scale.get_units(10), 2);//
#if ECHO_TO_SERIAL
Serial.print(m/60000); // milliseconds since start
Serial.print(" , ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\t Instant:\t");
Serial.print(scale.get_units(), 2);
Serial.print(" Average:\t");
Serial.print(scale.get_units(10), 2);

}
scale.power_down(); // put the ADC in sleep mode
delay(LOG_INTERVAL);
scale.power_up();
#endif //ECHO_TO_SERIAL
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL

// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();


{ digitalWrite(greenLEDpin, HIGH);// this was red led// blink LED to show we are syncing data to the card & updating FAT!
delay(500);// i just added this
digitalWrite(greenLEDpin, LOW);
} //turn off if combine leds was red led
delay(LOG_INTERVAL/1);// i just added this
logfile.flush();
} :mrgreen:
User avatar
By DanV
#201139
First, get rid of the delay()s in your program.
Research "Blink without delay".
Then you will be able to trigger your logging while waiting for the interval to expire.

Or, use an interrupt for the switch.
By mahardie
#201209
Sorry i dont understand what you mean. why do i need to trigger the logging independently of the delay ...? thanks. What code do i need to add the switch. How do i need to modify the code related to the log interval..?
User avatar
By DanV
#201213
If you did simple research on 'Blink without delay', you would realize that you can do nothing while the delay() is delaying.
As it stands, if you add a button to log your data, it will not be recognized while in delay(), where your program spends much of its time.