Notifications
Clear all

Use hardware interrupt to read a 5 button switch (DAOKI 5 button board)

27 Posts
5 Users
15 Likes
1,365 Views
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @eliza

I use a 5 key board attached to A5 and get values from zero to around 1000 from an analog pin. I use simple if statements to determine which button was pressed, like if(input > 160 && input < 170) return keyRight

I'd like to use an interrupt to catch the keydown event and read the pin inside the ISR like:

volatile int rawData; // global


volatile int globalStateVariable;


void myISR(){

rawData = analogRead(2); // catch interrupt with CHANGE but read the value here

globalStateVariable  = MapValue(rawData);

}

 

-------------------

now I would have a switch statement in the Arduino main loop to handle the key press detected

Is it possible to read analog in an ISR or do I only get "button pressed is true"

Thanks!!

You've made me look at my view of interrupts.  I have never tried and wouldn't have tried to use an analog pin on an Interrupt.  I just assumed it couldn't be done.  But I'm now guessing you could trigger on a rising or sinking analog pin just as easily as you can on a digital pin.  I'll have to try it some time.

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @ronbentley1

It is always a balance isn't it - theory v.  Pragmatism. 

Ron B

Slap it together, if smoke doesn't come out, ship it! 🤣 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ron bentley reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6966
 

@inq If I can ever find the page where I saw the semaphore reference (another one) I might be able to provide some insight.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
ron bentley
(@ronbentley1)
Member
Joined: 2 years ago
Posts: 385
 

@inq 

Have a look at freertos for Arduino and ESP 32. it's very functional and supports semaphores and lots more 

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
ReplyQuote
ron bentley
(@ronbentley1)
Member
Joined: 2 years ago
Posts: 385
 

@inq 

Clearly a pragmatist.

I approve!!!!

👍

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
ReplyQuote
(@codeslinger)
Member
Joined: 4 years ago
Posts: 30
 

@inq The problem with "try it, abuse it and see if bad behavior erupts." is that you are setting yourself up for something bad to happen when you least expect it.  My answers above are based on forty years of hard-won experience and NOT popular opinion.  Sometimes, the prevalent attitued expressed on the internet are based on the knowledge and experience of a lot of people who know what they are doing and should not be shrugged off lightly.  You will find that you can get away with ignoring good practice until you don't.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @codeslinger

@inq The problem with "try it, abuse it and see if bad behavior erupts." is that you are setting yourself up for something bad to happen when you least expect it.  My answers above are based on forty years of hard-won experience and NOT popular opinion.  Sometimes, the prevalent attitued expressed on the internet are based on the knowledge and experience of a lot of people who know what they are doing and should not be shrugged off lightly.  You will find that you can get away with ignoring good practice until you don't.

Even though I did not check your old postings to see your level of expertise, I felt from this one post that you know what you were talking about.  I just felt your response was too cut and dry negative without siting more detail for someone who wanted to learn why as well as what not to do.  I/O was too vague.  Certainly Serial.printf is unacceptable and I new you were singling out analogRead, but I felt it needed to be clarified.  I see no trouble with doing digitalRead / digitalWrite I/O within the interrupt.  It is commonly done despite what the Internet commonly says about using flags.  I simply agreed with your assessment but restricted it to analogRead and gave tabular data to fully explain why your and my comment is valid.

If you feel slighted, it was not my intent. 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
(@codeslinger)
Member
Joined: 4 years ago
Posts: 30
 

@inq No, I was not taking it personally.  I was more reacting to your apparent brush-off of the advice as just something that everybody says.  I did say "DO NOT do anything that will cause the ISR to wait for something else to happen".  I did single out I/O because that was the category under discussion.  The problem with messing things up in interrupt handlers is that the side-effects can be indeterminate.  It can cause a failure in area A of the program one time and area B of the program another.  In other cases maybe nothing happens or nothing immediately noticeable happens.


   
ReplyQuote
(@eliza)
Member
Joined: 2 years ago
Posts: 81
Topic starter  

This item is resolved. I used 4 separate buttons and pin change interrupts on 4 port B pins and forgot about using the analog switch board.

I followed Bill's example from the Dronebot Workshop interrupt video.

This post was modified 2 years ago by Eliza

   
ReplyQuote
(@eliza)
Member
Joined: 2 years ago
Posts: 81
Topic starter  

Update:  I learned pin change interrupts from Bill's video and built it on an Uno. My actual project is on a mega which has 6 ordinary hardware interrupts. I needed 4 switches. So, I went back and wrote 4 one liner ISR functions and attached them to Mega pins 18, 19, 20 and 21 and it worked like a charm. Indeed, it is quite charming as the project is "Tinkerbell In a Lantern" for a pirate magician. Tinkerbell dances and then reveals things magically.


   
Inq, ron bentley and Ron reacted
ReplyQuote
ron bentley
(@ronbentley1)
Member
Joined: 2 years ago
Posts: 385
 

@eliza 

Posts concerning ISRs always seem to bring out peoples passions!

Very pleased your sorted, sounds like an interesting application.

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @eliza

Indeed, it is quite charming as the project is "Tinkerbell In a Lantern" for a pirate magician. Tinkerbell dances and then reveals things magically.

Will we get to see it?  👍 I'm always looking for projects for my first grand-child.  Kids are so different these days, where when I grew up in a time where a piece of paper could build you the Concord and flights across the Atlantic were a simple throw away.  At least... until trying to achieve super-sonic too many times ended in auger-in.  

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Page 2 / 2