Jen-in-Wellies Posted January 30, 2021 Report Share Posted January 30, 2021 4 minutes ago, nicknorman said: And better to do asynchronous inputs such as button presses, by means of interrupts. I just checked and the Arduino Nano does support interrupts. Only two pins available, but excellent for inputs you really don't want to miss. Link to comment Share on other sites More sharing options...
system 4-50 Posted January 30, 2021 Author Report Share Posted January 30, 2021 Worth looking at the Ard IDE examples>button & debounce perhaps? The absolute rule for asking a code question is to publish the whole code. Formatting it nicely also helps to get an answer. Link to comment Share on other sites More sharing options...
davidb Posted January 30, 2021 Report Share Posted January 30, 2021 Perhaps I out to upload the sketch somewhere rather than cluttering this forum with it? But I haven't a lot of time at the mo, so I shall do that tonight and reply to your questions. if (buttonOld ==0 && buttonNew==1); is part of a trial which I haven't finished yet. There is a version connected to the boiler which works properly without any working code for the button. Thanks for your very quick replies, here is the full sketch to be going on with: int myPin = A2; // INPUT Analogue Sensor int stoke = 2; // INPUT Stoking Temp Sensor (Bottom of store) int houseUFH = 3; // OUTPUT UFH Relay int runPin = 4; // INPUT Heat Available Temp Sensor (Top of store) int annex = 5; // INPUT Annex Pump int house = 6; // INPUT House Pump int alarm = 7; // OUTPUT Alarm Light Relay int clok = 8; // INTPUT from Clock int veryHot = 9; // INPUT Boiler too hot int logStoreUFH = 10; // OUTPUT Log Store UFH Relay int button = 11; // alarm cancel button int ledPin = 13; // OUTPUT Indicator LED int buttonNew ; int buttonOld=1; int dt = 500; // Delay of 500secs void setup() { Serial.begin(9600); pinMode(myPin,INPUT); // pin A2 pinMode(stoke,INPUT); // stat acts 70deg pin 2 pinMode(houseUFH, OUTPUT); // Yellow Relay Wire pin 3 pinMode(runPin, INPUT); // stat acts at 56deg pin 4 pinMode(annex,INPUT); // pin 5 pinMode(house,INPUT); // pin 6 pinMode(alarm, OUTPUT); // Blue Relay Wire pin 7 pinMode(clok, INPUT); // 5v from clock pin 8 pinMode(veryHot, INPUT); // stat acts at 85deg pin 9 pinMode(logStoreUFH, OUTPUT); // Green Relay Wire pin 10 pinMode(button, INPUT); // push button pin 11 pinMode(ledPin, OUTPUT); // Indicator LED pin13 } void loop() { int analog = analogRead(myPin); // Reads data from myPin (A2) pin and puts in analog Int int readRun = digitalRead(runPin); // Reads data from runPin (4) and puts it in readRun Int int readStoke = digitalRead(stoke); // Reads data from stoke (2) and puts in readStoke Int int readAlarm = digitalRead(alarm); // Reads data from alarm (7) and puts in readAlarm Int int readClock = digitalRead(clok); // Reads data from clok (8) and puts it in readClock Int int readHot = digitalRead(veryHot); // Reads data from veryHot (9) and puts it in readHot Int int readBut = digitalRead(button); // Reads data from button (11) and puts it in readBut Int if (readRun == 1 && readClock == 1){ Serial.println("House UFH Turned On"); delay(dt*2); digitalWrite(houseUFH,LOW); // Turns House UFH on pin3 } if ((readRun == 0) || (readClock == 0)){ digitalWrite(houseUFH,HIGH); // Turns House UFH off if readRun OR readClock == 0 } if (readStoke == 0){ Serial.println("Boiler Requires Stoking"); Serial.println( ); digitalWrite(alarm,LOW); // turns alarm light on digitalWrite(ledPin,HIGH); delay(dt*3); digitalWrite(ledPin,LOW); // alarms off and will now flash repeatedly digitalWrite(alarm,HIGH); delay(dt*8); } else{ digitalWrite(ledPin,LOW);digitalWrite(alarm,HIGH); //alarm off } buttonNew = digitalRead(readBut); if (buttonOld ==0 && buttonNew==1); if (readStoke ==00){ digitalWrite (alarm, HIGH); } if (readBut == 1){ Serial.println("button pressed"); delay(dt*2); digitalWrite(alarm,HIGH); // Turns alarm relay off pin 7 delay(dt*10); } if (readBut == 0){ digitalWrite(alarm,HIGH); // keeps alarm Relay off } if (readHot == 1){ Serial.println("Store Pump Turned On"); delay(dt*6); digitalWrite(logStoreUFH,LOW); // Turns Store Pump relay on pin 10 } else{ digitalWrite(logStoreUFH,HIGH); // Turns Store Pump Relay off } } Post crossed, I'll answer tonight, thanks, David Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 A lot of delay in there, you would have to hold the button down for up to 7 seconds or so for it to be guaranteed to be read. Which even if it would work after holding for 7 seconds, isn't great programming! Reading buttons, which have inherent contact bouncing that lasts several mS, isn't the trivial task it might at first seem. At the very least you need to read the button frequently throughout the code, if it is pressed set a variable, if it is not pressed do nothing, then sample the variable at some point to see if it has been set, carry out the action and clear the button-pressed variable. Or as I said, the best way is to do it within an interrupt service routine (with a short delay in the routine, maybe 20mS, to allow for contact bounce). Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 8 minutes ago, nicknorman said: A lot of delay in there, you would have to hold the button down for up to 7 seconds or so for it to be guaranteed to be read. Which even if it would work after holding for 7 seconds, isn't great programming! Reading buttons, which have inherent contact bouncing that lasts several mS, isn't the trivial task it might at first seem. At the very least you need to read the button frequently throughout the code, if it is pressed set a variable, if it is not pressed do nothing, then sample the variable at some point to see if it has been set, carry out the action and clear the button-pressed variable. Or as I said, the best way is to do it within an interrupt service routine (with a short delay in the routine, maybe 20mS, to allow for contact bounce). I second that, just had to think really hard about my debounce code ? Bounce can occur on the down and the up so even if you use an interrupt routine you will still need additional time delay code. The millis() function is dead handy for this sort of thing, and more flexible than a simple delay. ..............Dave Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 39 minutes ago, Jen-in-Wellies said: Only two pins available, but excellent for inputs you really don't want to miss. Yes Arduinos are rubbish, I don't know why so many people use them. With the PICs that I use, every IO pin can have an interrupt, rising, falling or both, every peripheral can be mapped onto every pin, (on the smaller ones, or at least a couple of ports worth on the larger ones). Micros that have dedicated pins for dedicated peripherals are crap! Link to comment Share on other sites More sharing options...
Jen-in-Wellies Posted January 30, 2021 Report Share Posted January 30, 2021 3 minutes ago, nicknorman said: Yes Arduinos are rubbish, I don't know why so many people use them. With the PICs that I use, every IO pin can have an interrupt, rising, falling or both, every peripheral can be mapped onto every pin, (on the smaller ones, or at least a couple of ports worth on the larger ones). Micros that have dedicated pins for dedicated peripherals are crap! On the plus side, they are easy to learn for newcomers, with a nice shallow learning curve to get to some sophisticated results. The gateway drug of microcontrollers! ? 1 Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 Athy is reading. That could be interesting! 1 1 Link to comment Share on other sites More sharing options...
davidb Posted January 30, 2021 Report Share Posted January 30, 2021 Thanks again, Perhaps I need to read up on interrupts before I pursue this further? I have no idea about them, but have an idea that they have something to do with computer keyboards? I have zero experience of programming since my ZX81 ! So I have a very long way to go, but it has been most satisfying to find that what I have done so far actually works, and works well. https://www.youtube.com/watch?v=aMato4olzi8&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=28&t=802s "buttonNew = digitalRead(readBut); if (buttonOld ==0 && buttonNew==1); " comes from the lesson above, and I wondered if I could adapt it for my purpose. Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 6 minutes ago, davidb said: Thanks again, Perhaps I need to read up on interrupts before I pursue this further? I have no idea about them, but have an idea that they have something to do with computer keyboards? I have zero experience of programming since my ZX81 ! So I have a very long way to go, but it has been most satisfying to find that what I have done so far actually works, and works well. https://www.youtube.com/watch?v=aMato4olzi8&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=28&t=802s "buttonNew = digitalRead(readBut); if (buttonOld ==0 && buttonNew==1); " comes from the lesson above, and I wondered if I could adapt it for my purpose. Interrupts perhaps seem daunting but they are quite easy to implement. What they amount to is that when interrupts are enabled and an interrupt is triggered eg by a change of state of a pin, instantly the programme stops what it’s doing and switches to the interrupt service routine which is a separate bit of code. This could for example change the value of a variable to reflect the fact that the button has been pressed and perhaps carry out whatever action you want the button press to achieve. Once it has finished doing its thing, a return from interrupt is executed and the programme goes back to whatever it was doing at the moment the interrupt was triggered. Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 I have had limited success with Arduino interrupts, some code just won't execute correctly in interrupt routines for reasons that I never got to the bottom off. I have used interrupts fired from a timer but used the interrupt just to set a flag and then polled this flag from the main loop, which is not much better than a fully polled system working off millis(). ...........Dave Link to comment Share on other sites More sharing options...
Jen-in-Wellies Posted January 30, 2021 Report Share Posted January 30, 2021 (edited) Arranging a push button to turn an alarm off here doesn't really need interrupts. The key is to get rid of the huge number and lengths of delays in the sketch, so the program loops much more quickly. Quick enough to catch a button push change in level, then use one of several ways of coping with contact bounce. There isn't a lot going on in this application and none of it looks super time critical. Read up on using millis() to time events, rather than delay(). Lots of tutorials around. I've linked to the first two a search threw up. Jen Edited January 30, 2021 by Jen-in-Wellies Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 (edited) 1 hour ago, dmr said: I have had limited success with Arduino interrupts, some code just won't execute correctly in interrupt routines for reasons that I never got to the bottom off. I have used interrupts fired from a timer but used the interrupt just to set a flag and then polled this flag from the main loop, which is not much better than a fully polled system working off millis(). ...........Dave You are continuing to convince me that Arduinos are crap! Not wishing to teach grandma, but perhaps it is an issue with variables needing the ‘volatile’ keyword? I’m sure interrupts must work on the Arduino, otherwise there would be outcry. The advantage of the ISR to set flag, then poll flag in main routine, is that you can press the button any time, for a short time, and it will be captured, whereas with a fully polled system it might not be, unless you are wastefully polling the button very fast. Edited January 30, 2021 by nicknorman Link to comment Share on other sites More sharing options...
davidb Posted January 30, 2021 Report Share Posted January 30, 2021 You are are right there is nothing time-critical. in fact the delay for the button is going to be set to 20 minutes because it takes at least that long to regain it's heat. The overheat thermostat triggers the storing pump virtually immediately so that would have been an issue, but it definitely won't overheat when it is needing stoking. Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 4 hours ago, nicknorman said: I programme in C, not in Arduino-speak which is effectively C++, however: This line doesn’t seem to do anything: if (buttonOld ==0 && buttonNew==1); if you put a breakpoint after this line, does the variable reflect the state of the button? buttonNew = digitalRead(readBut); You have quite a few delays in the code, don’t forget the code reading the button only takes the instantaneous value, you have to be pressing and holding down the button at the same time as your code is actually reading the button. You talk about a pull-down resistor, so just to check that the button pulls the input line up to supply voltage? Yes I think as a general point, pull-up resistors are normally used, with the button pulling down to 0v, so the OP is doing things in the non-normal sense. But assuming no internal pull-ups are selected I suppose it should work. C and C++ are pretty much the same thing for most simple Arduino stuff. If you use some libraries then they do use a bit of Object stuff, but it can be mostly seen as just subroutine calls except for a single line to get the objects going (note absence of software speak here ? ). The "If" statement does look wrong (the && is just a logical And "if this AND that"). I would expect a curly brace and then some code after the IF. It looks to be testing for a button press but not doing anything about it. ...............Dave Link to comment Share on other sites More sharing options...
nicknorman Posted January 30, 2021 Report Share Posted January 30, 2021 1 minute ago, dmr said: C and C++ are pretty much the same thing for most simple Arduino stuff. If you use some libraries then they do use a bit of Object stuff, but it can be mostly seen as just subroutine calls except for a single line to get the objects going (note absence of software speak here ? ). The "If" statement does look wrong (the && is just a logical And "if this AND that"). I would expect a curly brace and then some code after the IF. It looks to be testing for a button press but not doing anything about it. ...............Dave Yes C and C++ are pretty similar. Of course in the context of Arduino, the function libraries mean that the hardware is kept at a distance. Which is good because it makes life simple. And bad because you don’t quite know what’s going on. Link to comment Share on other sites More sharing options...
Chris Lowe Posted January 30, 2021 Report Share Posted January 30, 2021 Interesting thread apart from excel macros I've not done programming since I used a Texas TI59 for work. Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 5 minutes ago, nicknorman said: You are continuing to convince me that Arduinos are crap! But perhaps it is an issue with variables needing the ‘volatile’ keyword? I’m sure interrupts must work on the Arduino, otherwise there would be outcry. The advantage of the ISR to set flag, then poll flag in main routine, is that you can press the button any time, for a short time, and it will be captured, whereas with a fully polled system it might not be, unless you are wastefully polling the button very fast. I did not spend much time working out the interrupt problem, I suspect its I2C code that is not allowed in an interrupt handler because maybe that uses interrupts itself. I think the Arduino is ok, a bit more memory would be nice. For boaty stuff (not engine running related) then they can have a reasonably low current consumption. Many of the "better" microcontrollers are a bit power hungry, enough to just start to matter if running 24 hours/day. Some of the Tiny family look quite good and can be used from the Arduino IDE. .............Dave Link to comment Share on other sites More sharing options...
Amos Moses Posted January 30, 2021 Report Share Posted January 30, 2021 I would say the issue is with what is triggering the alarm. you say if (readStoke == 0.... But to cancel the alarm you are just silencing the alarm, not resetting the input. So next time is goes around the loop, readStoke will still be 0 as the boiler is still cold. So, this will cancel out pushing the button. You have another issue - 11 delays if readStoke = 0, which is 11 x 500 seconds? So, again, if the program is held in the delay and you push the reset button, it will only see this once out of the delay. Jen-in-Wellies has pegged this and I would refer you to her comments. I would suggest setting a push button "flag" and looping faster without delays. So, if we have a variable "PushButtonFlag" and set this to 0 at the start. Then, if readStroke == 0 and PushButtonFlag = 0 (second if) only then flash the alarm. If you then push the button, this sets PushButtonFlag to 1 so the flashing loop is bypassed. You just need to reset the PushButtonFlag when readStroke == 1 again (i.e. if both are 1). Just suggesting.... Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 1 minute ago, buccaneer66 said: Interesting thread apart from excel macros I've not done programming since I used a Texas TI59 for work. C is a pretty horrid language to learn, lots of funny symbols rather than words, but the Arduino project is very interesting and very low cost, if you ever get bored on a dark winter night then get one and have a play, its always good to learn new skills. I rather suspect that some Sterling stuff is based on an Arduino type technology. ...........Dave Link to comment Share on other sites More sharing options...
Chris Lowe Posted January 30, 2021 Report Share Posted January 30, 2021 1 minute ago, dmr said: C is a pretty horrid language to learn, lots of funny symbols rather than words, but the Arduino project is very interesting and very low cost, if you ever get bored on a dark winter night then get one and have a play, its always good to learn new skills. I rather suspect that some Sterling stuff is based on an Arduino type technology. ...........Dave I may well have a go I used to enjoy tinkering with home electronics, still got pots of components. Link to comment Share on other sites More sharing options...
Amos Moses Posted January 30, 2021 Report Share Posted January 30, 2021 I am testing out the new Raspberry Pi Pico for easier programming. Hopefully. Link to comment Share on other sites More sharing options...
davidb Posted January 30, 2021 Report Share Posted January 30, 2021 3 minutes ago, buccaneer66 said: I may well have a go I used to enjoy tinkering with home electronics you will enjoy it - the hours fly by, but you will have something to show at the end of it! Link to comment Share on other sites More sharing options...
dmr Posted January 30, 2021 Report Share Posted January 30, 2021 8 minutes ago, nicknorman said: Yes C and C++ are pretty similar. Of course in the context of Arduino, the function libraries mean that the hardware is kept at a distance. Which is good because it makes life simple. And bad because you don’t quite know what’s going on. You can see the library source code if you want to understand what is going on, or even customise it a bit, or just copy the useful bits directly into your own code. Most of the libraries are very nicely/properly written in a object oriented/computer science style (abstraction etc etc) and I am not convinced this is good when memory is in such short supply. Sometimes good old dirty code is the way to go ?. ................Dave Link to comment Share on other sites More sharing options...
TheBiscuits Posted January 30, 2021 Report Share Posted January 30, 2021 (edited) 14 minutes ago, dmr said: C is a pretty horrid language to learn May I do the "C makes you feel sick that's why you boat on the canal" joke again? Or should I just provide a *pointer to it? https://www.canalworld.net/forums/index.php?/topic/106352-water-tank-meter/&do=findComment&comment=2477713 Edited January 30, 2021 by TheBiscuits Add a bit 2 Link to comment Share on other sites More sharing options...
Featured Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now