I have the following sketch, https://github.com/ipatch/KegCop/blob/master/KegCop-Bluno-sketch.c that I'm running on Arduino UNO compatible microcontroller. However I am trying to print statements while the flowmeter is in use with the following function,
// flowmeter stuff
bool getFlow4() {
// call the countdown function for pouring beer
// Serial.println(flowmeterPin);
flowmeterPinState = digitalRead(flowmeterPin);
// Serial.println(flowmeterPinStatePinState);
volatile unsigned long currentMillis = millis();
// if the predefined interval has passed
if (millis() - lastmillis >= 250) { // Update every 1/4 second
// disconnect flow meter from interrupt
detachInterrupt(0); // Disable interrupt when calculating
// Serial.print("Ticks:");
Serial.print(numTicks);
// numTicks = 0; // Restart the counter.
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, count, FALLING); // enable interrupt
}
if(numTicks >= 475 || valveClosed == 1) {
close_valve();
numTicks = 0; // Restart the counter.
valveClosed = 0;
return 0;
}
}
However, the print statements only get updated once the flowmeter has stopped operating. Any help would greatly be appreciated. As I would like the print statements to be updated while the flowmeter is in use.
Related
I am facing a strange issue while programming PIC18f45k22. I am initiating system modules and then I write a blocking delay for 3 seconds to wait for a touch screen to start (because I need to send some information to this screen). But when I do this I discovered that when I initiate timer before writing the delay, I discover that the system freezes.
This is the code that I used
NOTE: all the functions work fine without any problem.
void main(void) {
Sys_Init();
__delay_ms(3000);
while(1){
//Code
}
System initiation code
void Sys_Init(void) {
Delay_XTAL();
OSCILLATOR_Init();
DIGITAL_PIN_STATE();
ANALOG_PIN_STATE();
ADC_INIT(); //change clock select pins when oscillator changes
Spi_Init();
DHT22_init();
Uart_Init(9600); //initiate UART (change baud values when oscillator value changed)
Timer1_Init(); //initiate timer one (change prescaler and counter value if oscillator value changes)
}
Timer code
void Timer1_Init(void){ //for interrupt program use this function
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 1 ms
T1CON = 0x01;
TMR1IF = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE = 1;
INTCON = 0xC0;
}
Interrupt code
void __interrupt() ISR (void){
Timer1();
}
void Timer1(void){ //for interrupt program
if (TMR1IF){
cnt1++;
TMR1H = 0xF0; //overflow every 1 ms
TMR1L = 0x60;
TMR1IF = 0;
}
}
I have a PIC12F1822 of which there is an LED and relay as an output and a trigger as an input.
When the trigger is equal to one, I would like the to have the LED and relay stay turned on for 90 minutes.
The problem is that I cannot use a delay since __delayms has a limit.
How can this be done?
#include <xc.h>
#define _XTAL_FREQ 4000000
#define LED PORTAbits.RA5
#define RELAY PORTAbits.RA4
#define TRIGGER PORTAbits.RA2
int main()
{
// OSCCON = 0b01101000 ;
OSCCONbits.IRCF = 0b1101 ;
TRISAbits.TRISA5 = 0; //LED Output PIN
TRISAbits.TRISA4 = 0; //Output for Relay
TRISAbits.TRISA2 = 1; //INPUT trigger from comparator
while(1)
{
if (TRIGGER == 1)
{
LED = 1; // LED ON
__delay_ms(1000); // 1 Second Delay
LED = 0; // LED OFF
__delay_ms(1000); // 1 Second Delay
}
else
{
LED = 0; // LED OFF
}
}
return 0;
}
why not wrap the 1s delay in a function providing a delay_s(uint32_t seconds) and in a function providing a delay_m(uint32_t minutes)
void delay_s(uint32_t seconds)
{
while(seconds){
__delay_ms(1000);
seconds--;
}
}
void delay_m(uint32_t minutes)
{
while(minutes){
delay_s(60);
minutes--;
}
}
But:
You should be aware that this totally blocks your µC's program.
It will not react on any key press. This may be ok but may be you want the delay to be reset to 90min if the key is pressed during the delay.
I would suggest to implement the whole thing with interrupts and timers.
Set up a timer that triggers once every second and countdown a global variable (volatile!) within the isr that was set by your main loop. If the counter reaches 0 your isr may disable your output.
This way your µC can process other things in the mainloop and is not blocked by a __delay-function.
Simple. State machines and timer interrupts and a minute countdown. Something like this:
Disclaimer - coded blindly
#define MINUTES_90 5400UL /* 90 seconds */
typedef enum
{
/** Code operational */
p_state_idle,
/** Code waiting for 90 minutes */
p_state_waiting
}p_state_t;
static unsigned long gSecondsRemaining = MINUTES_90;
int main()
{
p_state_t gState = p_state_running;
OPTION_REGbits.PSA = 0; /* Prescaler assigned */
OPTION_REGbits.PS = 0b111; /* 256 prescaler */
OPTION_REGbits.TMR0CS = 0; /* Fosc / 4 */
INTCONbits.TMR0IE = 1; /* Timer 0 interrupt enabled */
INTCONbits.PEIE = 1; /* Peripheral interrupts enabled */
INTCONbits.GIE = 1; /* Global interrupts enabled */
/** Default LED off */
LED = 0;
while (1)
{
switch (gState)
{
case p_state_idle:
if (TRIGGER == 1)
{
LED = 1; // LED ON
gSecondsRemaining = MINUTES_90; /* Reset timer countdown */
gState = p_state_waiting;
}
break;
case p_state_waiting:
/** can sleep here */
if (gSecondsRemaining == 0)
{
gState = p_state_idle;
LED = 0;
}
break;
}
}
}
void interrupt ISR()
{
static unsigned char gSecond = 15;
/** approx 15 Hz ? */
if (INTCONbits.TMR0IF)
{
INTCONbits.TMR0IF = 0;
if (gSecond > 0)
gSecond--;
if (gSecond == 0)
{
if (gSecondsRemaining > 0)
gSecondsRemaining--;
gSecond = 15;
}
}
}
You could solve this with an inline assembly function with some loops. Have a look here: example: 30 min delay
So i have rewritten my code. when I press the button connected on pin 2 it makes pin 13 HIGH and it sends a signal via a transceiver to a receiver (type of transceiver and receiver is irrelevant). I connected a wire from the receiver(where pin 13 makes it HIGH) to pin 7 on the arduino. Also I connected a LED to pin 8 to indicate when pin 7 is HIGH.
My main focus is to calculate the time it took from pressing the button until pin 7 is made HIGH on the Arduino. I am using a Arduino Leonardo( also irrelevant information).
This is my code :
int buttonState;
int buttonPin = 2;
int LbuttonState; // last button state
int pin13 = 13;
int pin7state;
int pin7 = 7;
int Lpin7state; // last pin 7 state
int pin8 = 8;
long startTimeKeeper;
long endTimeKeeper;
long elapsedTime;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(pin13, OUTPUT);
pinMode(pin7, INPUT);
pinMode(pin8, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && LbuttonState == LOW) {
startTime(); // start the time
digitalWrite(pin13, HIGH);
LbuttonState = buttonState;
} else if(buttonState == HIGH && LbuttonState == LOW) {
digitalWrite(pin13, LOW);
} else if(buttonState == LOW && LbuttonState == HIGH) {
digitalWrite(pin13, LOW);
LbuttonState = buttonState;
} else//(buttonState == LOW && LbuttonState == LOW)
digitalWrite(pin13, LOW);
pin7state = digitalRead(pin7);
if(pin7state == HIGH && Lpin7state == LOW) {
stopTime(); // stop the time
digitalWrite(pin8, HIGH);
Lpin7state = pin7state;
} else if(pin7state == HIGH && Lpin7state == HIGH) {
digitalWrite(pin8, HIGH);
} else if(pin7state == LOW && Lpin7state == HIGH) {
digitalWrite(pin8, LOW);
Lpin7state = pin7state;
} else//(pin7state == LOW && Lpin7state == LOW)
digitalWrite(pin8, LOW);
}
void startTime() {
startTimeKeeper = millis();
}
void stopTime() {
endTimeKeeper = millis();`enter code here`
elapsedTime = endTimeKeeper - startTimeKeeper;
Serial.print(elapsedTime);
}
I would suggest using interrupts, especially since the Leonardo supports interrupts triggered on state changes of both of your chosen pins.
If I understand the core of the problem correctly, you want the time elapsed between a falling edge (HIGH to LOW) from your button press on pin 2 to a rising edge (LOW to HIGH) on pin 7. If I misinterpreted that and your button is actually active-high, just change the final parameter of attachInterrupt(interrupt, ISR, mode) to RISING.
After these are setup, our specified interrupt service routine (ISR) functions will be called whenever the specified state or state change occurs. We want to do minimal work in these ISRs since no other ISR can be triggered while one is running. Recording a start or stop time would be fine.
However, interrupts cannot use millis() or micros() directly because those functions themselves use interrupts. To work around that constraint, we'll toggle a simple flag of our own in each ISR--to indicate that it was triggered--then poll for that flag value in the main loop, where we'll do our timer start/stop actions. I subbed-in micros() for better accuracy since the time between a button press and a signal received should be small (never on the order of minutes, anyway).
#define ULONG_MAX 0xFFFFFFFFUL
unsigned long startTimeKeeper, stopTimeKeeper, elapsedTime;
volatile boolean buttonFlag = false, signalFlag = false;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(7, INPUT);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
// Int.2 corresponds to pin 2
attachInterrupt(2, buttonPressed, FALLING);
// Int.4 corresponds to pin 7
attachInterrupt(4, signalReceived, RISING);
}
void loop() {
// Loop until the buttonPressed ISR sets this flag
if (buttonFlag) {
// Record the start time
startTimeKeeper = micros();
// Do nothing until the signal flag is set by the ISR
while (!signalFlag);
// Record the end time
stopTimeKeeper = micros();
// Normal case - stop time is apparently after start time
if (stopTimeKeeper > startTimeKeeper)
elapsedTime = stopTimeKeeper - startTimeKeeper;
// Overflow case - stop time is apparently before start time
else
elapsedTime = stopTimeKeeper + (ULONG_MAX - startTimeKeeper);
Serial.print(elapsedTime);
signalFlag = buttonFlag = false;
}
}
// Very lightweight ISRs
void buttonPressed() {
buttonFlag = true;
}
void signalReceived() {
signalFlag = true;
}
Since we immediately begin waiting for the signalReceived() ISR to activate signalFlag after we register a button press, we don't have to worry too much about debouncing the switch in this case.
In general, you'll want to debounce your switches, either with physical circuitry or software counters. Check out this tutorial to get started with software, or look here for information on building debounce circuits.
So I hacked at a getFlow function for the Arduino sketch I am working on, and I am getting output of the function in the console of the iPhone, but when I hook up the computer to the Arduino, and open the serial monitor I don't see any output from the getFlow4 function. I am using a Swiss flow SF800 flowmeter / flowsensor.
/*
* kegboard-clone-4-KegCop
* This code is public domain
*
* This sketch sends a receives a multibyte String from the iPhone
* and performs functions on it.
*
* This Arduino sketch is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Public License
* along with this sketch. If not, see <http://www.gnu.org/licenses/>.
*
* Examples:
* http://arduino.cc/en/Tutorial/SerialEvent
* http://arduino.cc/en/Serial/read
* http://stackoverflow.com/questions/16532586/arduino-sketch-that-responds-to-certain-commands-how-is-it-done/
* http://davebmiller.wordpress.com/2011/01/18/arduino-flowmeter/
* http://forum.arduino.cc/index.php?topic=52003.0
* http://arduino.cc/en/Reference/AttachInterrupt
*
* TODO:
* - eventually get code working with the SF800 flow sensor / flowmeter
*
*/
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// string / serial event variables
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean valve_open = false;
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
// the SF800 outputs 5400 pulses per litre
// The hall-effect flow sensor (SF800) outputs approximately 5400 pulses per second per litre/minute of flow
int sensorInterrupt = 0; // changed from byte
int sensorPin = 2; // changed from byte
int sensorPinState = 0; // variable for storing state of sensor pin
// read RPM
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
// Serial.flush(); // flush the serial buffer on setup.
Serial.begin(115200); // open serial port, sets data rate to 9600bps
Serial.println("Power on test");
inputString.reserve(200);
valve_open = false;
// relay for solenoid cut off valve
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH); // Need to set these HIGH so they won't just tick away
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a RISING state change (transition from HIGH
// state to LOW state)
attachInterrupt(0, rpm_fan, RISING);
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
valve_open = true;
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
valve_open = false;
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
void flow_A_on() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
}
void flow_A_off() {
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
// flowmeter shit
void getFlow4() {
// Serial.println("im here");
// Serial.println(sensorPin);
sensorPinState = digitalRead(sensorPin);
// Serial.println(sensorPinState);
if (millis() - lastmillis >= 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(rpmcount); //print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, RISING); //enable interrupt
}
if(sensorPinState == LOW) {
flow_A_off();
Serial.println("don't blink");
}
if(sensorPinState == HIGH) {
flow_A_on();
Serial.println("blink damnit");
}
if(stringComplete) {
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
return;
}
}
void rpm_fan(){
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("inputString equates :)");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
if(valve_open) {
// Serial.println("valve_open = true");
inputString = "";
stringComplete = false;
getFlow4();
}
// clear the string:
inputString = "";
stringComplete = false;
}
//Serial.println("over and over");
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}
So I'm trying to count the pulses of a swiss flow SF800 flow sensor / meter in my current Arduino project, but I'm currently not getting any data printed out to the console. My Arduino sketch looks like the following,
// global variables should be identified with _
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
Serial.println("reached getFlow function");
if(millis() - lastmillis == 1000) { // Update every one second, this will be equal to reading frequency (Hz).
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
getFlow();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}
The following code changes seem to be printing the RPM and Hz values now.
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
// Serial.println("reached getFlow function");
if(millis() - lastmillis >= 1000) { // Update every one second, this will be equal to reading frequency (Hz). Using >= should be safter
// Serial.println("reached inside if statement");
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
getFlow();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}