Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
Reading different analogin in main and Ticker causes crash
Hi,
So basically I am using a ticker which reads an analog pin every so often. When a certain button is pressed, the main goes into a if loop where a different analog pin is read. As soon as that button is pressed the program seems to freeze and the system becomes unresponsive. Through a bit of testing I have come to the conclusion that its the analogIn that is the problem. Here is a simplified program of what I'm using:
#include "mbed.h" DigitalIn Button1(p21); DigitalOut led1(LED1); AnalogIn Joystick1(p18); AnalogIn Ain3(p15); float LFT; int LowBatteryCounter=0; Ticker VoltageCheck_tick; void LowBatteryShutDown(void); int main() { VoltageCheck_tick.attach(&LowBatteryShutDown,0.5); while(1) { if (Button1==1) { LFT = Joystick1; } } } void LowBatteryShutDown(void) { led1 = !led1; if(Ain3<=0.81) { LowBatteryCounter++; } }
Anyone have any idea why this is happening and how to get around this?
Thanks
1 Answer
9 years, 1 month ago.
Adding a 'wait' or some more code here makes it work, try this. Also add pull-ups or downs on switch/button inputs, noise can cause problems.
led2 will flash when p21 goes high. Sam or Erik will explain why this works.
Snip
while(1) { if (Button1==1) { LFT = Joystick1; led2=!led2; } wait_ms(100); }
This happens every time? I can see if one AnalogIn operation interrupts another one that it can give a problem. But with your wait/ticker times the chance of that happening is extremely small.
posted by Erik - 24 Feb 2015ye every time, right away
posted by Johann Hur 24 Feb 2015Can you strip out all code that doesn't have to do with buttons (DigitalIn) and AnalogIn and then publish the program as a test harness to point out the root cause failure? These snippets are great to review code but the library version is not tracked. Just helps others test and reproduce a little faster!!
posted by Sam Grove 24 Feb 2015ok cut some more stuff out
posted by Johann Hur 24 Feb 2015Made the code as simple as I could to reproduce the problem. http://developer.mbed.org/users/lanfear/code/test2/
posted by Johann Hur 26 Feb 2015