Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #15 on: December 03, 2012, 10:57:24 am » |
how do I call the counting code after the interrupts have occured so as to display the count/counter (incrementing or decrementing) on the 3 digit seven segment display??
You have a variable that contains the value you last sent to the display. At the start of the loop you compare this variable with the count variable. If they are not the same you update the display.
|
|
|
|
« Last Edit: December 03, 2012, 05:47:40 pm by Grumpy_Mike »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #16 on: December 03, 2012, 12:25:39 pm » |
Could you give me a starter please? am starting my display with 000, increment and decrement accordingly as the sensors makes and breaks I need to understand this more please.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19034
I don't think you connected the grounds, Dave.
|
 |
« Reply #17 on: December 03, 2012, 12:31:48 pm » |
It's a really bad idea to put serial I/O in interrupt routines. It's a good idea to post code inside code tags.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
the land of sun+snow
Offline
Edison Member
Karma: 81
Posts: 2123
|
 |
« Reply #18 on: December 03, 2012, 12:45:58 pm » |
I'm gonna repeat one thing I said previously, and then go away. I see no reason to mess with interrupts here. It's much easier to use polled I/O instead, and by far, the best way to learn programming is to learn the easy stuff first. You don't have 100 people a second going past the counter.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #19 on: December 03, 2012, 01:46:02 pm » |
What's wrong with using interrupts?can you elaborate pls.
thanks
|
|
|
|
|
Logged
|
|
|
|
|
the land of sun+snow
Offline
Edison Member
Karma: 81
Posts: 2123
|
 |
« Reply #20 on: December 03, 2012, 02:03:56 pm » |
As mentioned, when learning to program, it's generally easier to start with the simpler and more straightforward constructs, then once you gain some experience, go on to the more complex constructs. Then you don't get into the sort of troubles you're getting yourself into. This is all called the Learning Curve.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #21 on: December 03, 2012, 05:51:37 pm » |
Could you give me a starter please?
What is wrong with reply #15. Also as AWOL says do not print inside ISRs, it screws things up. If you want help do not ignore it when it is given. If you don't understand it, ask. Go back and modify that post with code in. Select the code and then hit the # icon. Then save it. That will put code tags around the code and we can copy the code into our own system to better help you.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 71
Posts: 6823
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #22 on: December 03, 2012, 08:20:13 pm » |
While I did suggest using interrupts I didn't suggest using 4 of them and all that attach/unattach stuff. My example was simplicity itself, it does however require that the input signal is clean with no bounce. Here's a rejigged version of what you have int last_counter = 0; volatile int counter = 0;
const int S2_pin = 50; // change to whatever pin it's on
void setup() { attachInterrupt(0, S1_RISE, RISING);//hooked to pin2 on mega 2560 Serial.begin(115200); }
void loop(){ if (counter != last_counter) { Serial.println(counter); counter = last_counter; } }
void S1_RISE(){
if (digitalRead(S2_pin) == HIGH) counter++; else counter--;
if (counter < 0) counter = 0; }
While I agree with oric_dan(333) that interrupts are not needed here if the input signal is clean I actually think interrupts are easier to use. That said I'll do a polled version as well shortly. EDIT @stefanoGizz What sensors are you using? Do they provide a clean bounce-free signal? ______ Rob
|
|
|
|
« Last Edit: December 03, 2012, 11:48:54 pm by Graynomad »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #23 on: December 04, 2012, 09:17:43 am » |
My infrared sensors do give very clean pulses since they are hooked to a schmitt trigger. My version of interrupts does work well as it captures all the 4 changes on the 2 pins of my sensors, having said that I still need help integrating the function to display 0-999 incrementing or decrementing as neccessary. I do take the advice from reply #15. I am also taking in all the advice so far given and much thanks to all.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 71
Posts: 6823
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #24 on: December 04, 2012, 09:30:41 am » |
I really don't see why you need all that interrupt stuff, but if it works so be it. I still need help integrating the function to display 0-999 incrementing or decrementing as neccessary Just to be clear, you currently have working code that prints to the serial monitor? ______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #25 on: December 04, 2012, 10:31:39 am » |
Hi Rob,
That's true, I can print on the serial monitor and that's where I can see the counting up/down. And I also have the code that counts from 0 to 999 back to 0. All I want to know is how to fix the counting code to be called upon the interrupts occurring so as to update the counter and hold that count till the next sequence of interrupts. Am glad you are willing to help. Thanks.
Steffano.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #26 on: December 04, 2012, 10:38:51 am » |
Mike,
I have been thinking of your reply and I feel it makes sense. Can you please insert an example on my ISR code attached? this will be a starter for me, bearing in mind that I have the code that displays 000---999-000, how to call it is a bit of a challenge to me as a newbie.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #27 on: December 04, 2012, 11:38:56 am » |
What we need to know is how you have the seven segment displays wired up. That schematic you posted did not show what pins were connected to what. Then before you update the display you have to have code that will multiplex the display, do you have that? We need to see it. Is it written correctly, there is a lot of rubbish about with very verbose code. This link shows you how to do it properly. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html But you have to extend it to cope with the multiplexing.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #28 on: December 04, 2012, 01:55:01 pm » |
Graynomad,
I have tried to go your way and it seems that I need more modification as your example code doesn't give me anything on the serial monitor.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #29 on: December 04, 2012, 03:46:40 pm » |
I have also noted that your ISR sketch was more easier Odd as I don't have an ISR on that page. Yes that sort of code was just the sort I warned against. So have a good read of that page and write the refresh code properly.
|
|
|
|
|
Logged
|
|
|
|
|
|