how can I get this code to reset the Millis after the timer has expired?
#include <HX711.h>
#include <SD.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define BREAKREMINDERS 15000//Second break reminder
#define BREAKREMINDERT 30000// Third break reminder
#define BREAKREMINDER 8000
#define WEIGHT_THRESHOLD 4 //Threshold to count for person sitting
#define SD_ChipSelectPin 4 // SD CARD IN PIN 4
#define SPEAKER 9 // SPEAKER OUTPUT IN PIN 9
#define DOUT 3
#define CLK 2
TMRpcm tmrpcm;
HX711 scale;
float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup
float weight;
unsigned long StartTime; //Sitting timer
const unsigned long Interval = 3000;
void SetupSound() {
// put your setup code here, to run once:
tmrpcm.speakerPin = SPEAKER;
tmrpcm.CSPin = SD_ChipSelectPin;
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("failed to read card..");
return;
}
tmrpcm.setVolume(5);
Serial.println("Card read success..");
}
void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");
scale.begin(DOUT, CLK);
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
StartTime = millis(); // Timer is not running
SetupSound();
}
void loop() {
if (StartTime == 0)
{
// Timer is not running. Check for condition to start the timer.
if (weight > WEIGHT_THRESHOLD)
StartTime = millis(); // Start the timer
}
else // Timer is running
{
if (millis() - StartTime >= Interval)
{
// Timer was running and has expired
StartTime = 0; // Stop the timer
// DO YOUR TIMER EXPIRED STUFF HERE
}
}
Serial.println (StartTime);
weight = scale.get_units();
weight = weight;
if ( weight < 12000 ) {
weight = 0;
}
if ( weight >= WEIGHT_THRESHOLD ) {
Serial.println("trigger..");
tmrpcm.play("123.wav");
}
if (( weight >= WEIGHT_THRESHOLD) && (StartTime > BREAKREMINDER )){
Serial.println("trigger2..");
tmrpcm.play("124.wav");
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
there is for me. I want to do what this guy did but for my code when the interval has passed
let me explain what im trying to do. when the counters reads 8000 ie 8 seconds it will play a sound and then at 15 seconds, it will play another sound. The code is not complete I only want this to occur once each when weight is detected and then have the process restart when I take the weight off wait until the interval has passed then put weight on again. Instead, the counter keeps going up and once i put weight on it plays both the sounds as the first sound is set to go off at 8000 and the second 15000 and the counter is now something like 23000 because it doesn't reset. that's why I need it to reset.
thanks for your post. I tried that but it's not working properly. Im just too noob it just starts without weight being selected then triggers after the time period then restarts but doesn't go to the second one or stop when weight is not detected.
I should probably explain what time trying to do. I am attaching a load cell to a computer chair with an Arduino secure attached under the seat. wired to the Arduino is a speaker and sd card that stores audio files for the speaker to play. When I sit on my seat I am going to make it say "Welcome". while sitting on the seat I want it to time how long I've been sitting. after a period of time, it will remind me to take a break. but restart this process once I've left for a short while.
Yes your right. And all help is always appreciated. thanks for taking the time to help me.
my code is here
#include <HX711.h>
#include <SD.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define BREAKREMINDERS 15000//Second break reminder
#define BREAKREMINDERT 30000// Third break reminder
#define BREAKREMINDER 8000 // first break reminder
#define WEIGHT_THRESHOLD 4 //Threshold to count for person sitting
#define SD_ChipSelectPin 4 // SD CARD IN PIN 4
#define SPEAKER 9 // SPEAKER OUTPUT IN PIN 9
#define DOUT 3
#define CLK 2
#define WAIT_FOR_WEIGHT 0
#define WAIT_FIRST_SOUND 1
#define WAIT_SEC_SOUND 2
#define RESET_WAIT 3
TMRpcm tmrpcm;
HX711 scale;
float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup
float weight;
int state = WAIT_FOR_WEIGHT;
unsigned long StartTime =0; //Sitting timer
const unsigned long Interval = 3000; //no weight wait period
void SetupSound() {
// put your setup code here, to run once:
tmrpcm.speakerPin = SPEAKER;
tmrpcm.CSPin = SD_ChipSelectPin;
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("failed to read card..");
return;
}
tmrpcm.setVolume(5);
Serial.println("Card read success..");
}
void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");
scale.begin(DOUT, CLK);
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
SetupSound();
}
void loop() {
weight = scale.get_units();
weight = weight;
if ( weight < 12000 ) {
weight = 0;
}
if (state == WAIT_FOR_WEIGHT )
{
// Timer is not running. Check for condition to start the timer.
if (weight > WEIGHT_THRESHOLD)
StartTime = millis(); // Start the timer
state = WAIT_FIRST_SOUND;
Serial.println (StartTime);
}
if (millis() - StartTime >= BREAKREMINDER)
{
// Timer was running and has expired
Serial.println("trigger..");
tmrpcm.play("123.wav");
StartTime = millis();
state = WAIT_SEC_SOUND;
// DO YOUR TIMER EXPIRED STUFF HERE
}
if(state == WAIT_SEC_SOUND){
if( millis() - StartTime > BREAKREMINDERS){
Serial.println("trigger2..");
tmrpcm.play("123.wav");
StartTime = millis();
state = RESET_WAIT;
}
}
if(millis() - StartTime > Interval){
state = WAIT_FOR_WEIGHT;
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
when I sit on my seat I want to time then play audio after the period first sound interval has passed then so on and have the entire process reset when I get off the seat for a short while. If that makes any sense.
If you look at the code that gets slowly typed over the first four minutes of the video you referenced you will see that he sets 'cur_time' to millis() - pre_time and sets pre_time to millis() when he wants to re-start the timer. At no time does he set millis() to zero.
Ive changed it to this and it now is getting closer to what I want but still not working right.
#include <HX711.h>
#include <SD.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define BREAKREMINDERS 6000//Second break reminder
#define BREAKREMINDERT 8000// Third break reminder
#define BREAKREMINDER 8 // first break reminder
#define WEIGHT_THRESHOLD 4 //Threshold to count for person sitting
#define SD_ChipSelectPin 4 // SD CARD IN PIN 4
#define SPEAKER 9 // SPEAKER OUTPUT IN PIN 9
#define DOUT 3
#define CLK 2
#define WAIT_FOR_WEIGHT 0
#define WAIT_FIRST_SOUND 1
#define WAIT_SEC_SOUND 2
#define RESET_WAIT 3
TMRpcm tmrpcm;
HX711 scale;
float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup
float weight;
int state = WAIT_FOR_WEIGHT;
unsigned long StartTime = 0; //Sitting timer
const unsigned long Interval = 3000; //no weight wait period
void SetupSound() {
// put your setup code here, to run once:
tmrpcm.speakerPin = SPEAKER;
tmrpcm.CSPin = SD_ChipSelectPin;
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("failed to read card..");
return;
}
tmrpcm.setVolume(5);
Serial.println("Card read success..");
}
void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");
scale.begin(DOUT, CLK);
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
SetupSound();
}
void loop() {
weight = scale.get_units();
weight = weight;
if ( weight < 12000 ) {
weight = 0;
}
if(state == WAIT_FOR_WEIGHT){
if (weight > WEIGHT_THRESHOLD) {
StartTime = millis(); // record the time
state = WAIT_FIRST_SOUND;
}
}
if(state == WAIT_FIRST_SOUND) {
if(millis() - StartTime >= BREAKREMINDER){
Serial.println("trigger2..");
tmrpcm.play("124.wav");
StartTime = millis(); //record a new time to time agains
state = WAIT_SEC_SOUND; // move to next state
}
}
if(state == WAIT_SEC_SOUND){
if(millis() - StartTime > BREAKREMINDERS) {
Serial.println("trigger2..");
tmrpcm.play("124.wav");
StartTime = millis(); // get a new time to time against
state = RESET_WAIT;
}
}
if(state == RESET_WAIT) {
if(millis() - StartTime > Interval){
state = WAIT_FOR_WEIGHT; // reset the state to wait for the next weight
// ddo anything else you want to do before you go around again.
}
}
// Here you can do things that you want to have happen all the time
// regardless of what state the stuff up there is in.
// like updating displays or blinking lights or whatever
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
What output are you getting (you can copy and paste from the Serial Monitor window) and how is it different from what you want?
There is a switch/case statement that is good for when you want to compare an integer variable (like 'state') to a selection of integer constants (like your state values). It make the code a little easier to read:
#include <HX711.h>
#include <SD.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define BREAKREMINDER 8 // first break reminder
#define BREAKREMINDERS 6000 // Second break reminder
#define BREAKREMINDERT 8000 // Third break reminder
const unsigned long Interval = 3000; //no weight wait period
#define SD_ChipSelectPin 4 // SD CARD IN PIN 4
#define SPEAKER 9 // SPEAKER OUTPUT IN PIN 9
#define DOUT 3
#define CLK 2
// 'enum' (enumeration) is an easy way to assign unique values to a bunch of names
// where you don't really care which values they get as long as they are unique:
enum States {
WAIT_FOR_WEIGHT, WAIT_FIRST_SOUND, WAIT_SEC_SOUND, RESET_WAIT
} state = WAIT_FOR_WEIGHT;
TMRpcm tmrpcm;
HX711 scale;
float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup
float weight;
#define WEIGHT_THRESHOLD 12000.0 //Threshold to count for person sitting
unsigned long StartTime = 0; //Sitting timer
void SetupSound() {
// put your setup code here, to run once:
tmrpcm.speakerPin = SPEAKER;
tmrpcm.CSPin = SD_ChipSelectPin;
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("failed to read card..");
return;
}
tmrpcm.setVolume(5);
Serial.println("Card read success..");
}
void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");
scale.begin(DOUT, CLK);
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
SetupSound();
}
void loop()
{
weight = scale.get_units();
switch (state)
{
case WAIT_FOR_WEIGHT:
if (weight > WEIGHT_THRESHOLD)
{
StartTime = millis(); // record the time
state = WAIT_FIRST_SOUND;
}
break;
case WAIT_FIRST_SOUND:
if (millis() - StartTime >= BREAKREMINDER)
{
Serial.println("trigger1..");
tmrpcm.play("124.wav");
StartTime = millis(); //record a new time to time against
state = WAIT_SEC_SOUND; // move to next state
}
break;
case WAIT_SEC_SOUND:
if (millis() - StartTime > BREAKREMINDERS)
{
Serial.println("trigger2..");
tmrpcm.play("124.wav");
StartTime = millis(); // get a new time to time against
state = RESET_WAIT;
}
break;
case RESET_WAIT:
if (millis() - StartTime > Interval) {
state = WAIT_FOR_WEIGHT; // reset the state to wait for the next weight
// do anything else you want to do before you go around again.
}
break;
}
// Here you can do things that you want to have happen all the time
// regardless of what state the stuff up there is in.
// like updating displays or blinking lights or whatever
}