I have two load cells, measuring 2 different weights.
Looks something like this:
(I have built in an LCD display, and Timer, I don't want to overflow the code with it, but if needed, I can copy it, but it's a maaasssssssssssss:D ).
My problem is, that it's sampling the loadcell too fast without a delay operation, but I don't want to use delay, because it screws up the timer. So I want to do it with an Interrupt which reads the current value every let say .5 sec and display it on the LCD display.
What interrupt should I use?
Timer/Counter?
ADC conversion complete?
My timer works with millis(), so it can count sec, minutes, will the other ISR screw up my timer?
Don't do it with interrupts. Do it like blink without delay.
Here's the blink without delay code adapted to do what you need. You just need to insert your code to read the load cell where indicated.
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last time load cell was read
// constants won't change:
const long interval = 500; // interval at which to read load cell
void setup() {
// do whatever you need to do
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to read the load cell; that is, if the difference
// between the current time and last time you read the load cell is bigger than
// the interval at which you want to read the load cell.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you read the load cell
previousMillis = currentMillis;
// CODE TO READ THE LOAD CELL GOES HERE
}
}
Okay, I think I start to understand this millis() thing, and Blink without delay().
My timer is build like this, the problem that my timer now skips seconds, ultimately it's okay in the long run, but the reading on the LCD display is a mess :S
I think I need to do it outside of the loop, so they don't get confused, right?
digitalWrite(ledPin, LOW); // Initiate LED and Step Pin States
startState = digitalRead(startPin); // Check for button press, read the button state and store
// check for a high to low transition if true then found a new button press while clock is not running - start the clock
if (startState == LOW && lastButtonState == HIGH && blinking == false) {
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
//delay(10); // short delay to debounce switch
lastButtonState = startState; // store startState in lastButtonState, to compare next time
}
// check for a high to low transition if true then found a new button press while clock is running - stop the clock and report
else if (startState == LOW && lastButtonState == HIGH && blinking == true) {
blinking = false; // turn off blinking, all done timing
lastButtonState = startState; // store startState in lastButtonState, to compare next time
// Routine to report elapsed time
elapsedTime = millis() - startTime; // store elapsed time
elapsedMinutes = (elapsedTime / 60000L);
elapsedSeconds = (elapsedTime / 1000L); // divide by 1000 to convert to seconds - then cast to an int to print
elapsedFrames = (elapsedTime / interval); // divide by 100 to convert to 1/100 of a second - then cast to an int to print
fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
lcd.setCursor(11,1);
if (fractionalMins < 10) { // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalMins, buf, 10)); // convert the int to a string and print a fractional part of 60 Minutes to the LCD
lcd.print(":"); //print a colan.
if (fractionalSecs < 10) { // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalSecs, buf, 10)); // convert the int to a string and print a fractional part of 60 Seconds to the LCD
}
else {
lastButtonState = startState; // store startState in lastButtonState, to compare next time
}
// run commands at the specified time interval
// blink routine - blink the LED while timing
// check to see if it's time to blink the LED; that is, the difference
// between the current time and last time we blinked the LED is larger than
// the interval at which we want to blink the LED.
if ( (millis() - previousMillis > interval) ) {
if (blinking == true) {
previousMillis = millis(); // remember the last time we blinked the LED
digitalWrite(ledPin, HIGH); // Pulse the LED for Visual Feedback
elapsedTime = millis() - startTime; // store elapsed time
elapsedMinutes = (elapsedTime / 60000L); // divide by 60000 to convert to minutes - then cast to an int to print
elapsedSeconds = (elapsedTime / 1000L); // divide by 1000 to convert to seconds - then cast to an int to print
elapsedFrames = (elapsedTime / interval); // divide by 40 to convert to 1/25 of a second - then cast to an int to print
fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
lcd.setCursor(11,1);
if (fractionalMins < 10) { // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalMins, buf, 10)); // convert the int to a string and print a fractional part of 60 Minutes to the LCD
lcd.print(":"); //print a colan.
if (fractionalSecs < 10) { // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalSecs, buf, 10)); // convert the int to a string and print a fractional part of 60 Seconds to the LCD
}
else {
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}
}
Should I get rid of the blinking statement? I don't know why it is there
XenoBen:
My timer is build like this, the problem that my timer now skips seconds,
That is probably because you update your timer all the time (and print out all the info every time)
Main thing, write to the lcd-screen only when something has changed and preferably only the parts that have changed.