Hi,..
the thing is, the hardware is horrendous.. I must admit I didn't build/design all of it myself.
I think the program looks ok on paper, but doesn't run.
In Java I'm used to compilers being fussy where you declare variables, in loops is a big no no for instance.. as it re assigns storage every time round the loop. Which makes for slow running programs. (Worse than that, it eats memory, as you keep all the old pointers till the garbage collector gets them)
I suppose I was just after someone to put an experienced eye over that bit of code, incase I'm doing anything that's a bit of a daft thing to do in C.
I *MUST* do a C course, the only one I started got a bit caught up in malloc etc etc.. things we don't really use in the mbed world afaik.
cheers,
Dave.
Hi all..
Here's an explanation, but you can probably jump straight to the code. (The code below is for a differential opAmp circuit. I can control a reference voltage, and by reading the output voltage deduce what the analogue input voltage is. I'm reading to 0.04v, and so I have a 10x multiplier on the opAmp, so I can accurately read the input. I'll explain if anyone is interested. But it works PERFECTLY)
This code runs perfectly in a while loop, on its own in a main. It is subsecond.
If I attach it to a ticker, and run it every 21 secs. Mbed hangs. I presume with a memory problem.
I'm new to C, being a Java programmer. This would all be fine in the languages I usually work in. What am I doing that's enough of a bad practise to cause mbed to hang ?
//main declarations. AnalogOut reference(p18); AnalogIn reading(p20); float ref=0.0; float aRead = 0.0; ..... in the method.... ref=2.6/3.3; //1800 steps to zero! //So we can get a stable reading in 1800 steps. //start at the top and work down till we find the reading. //Adjust the reference, when the output is stable at 0.3v. We can work out input. //as we know the hardware and the reference voltage. int i=0; while (i<1800) { if (aRead>0.30 && ref>0) { ref=ref-0.0005; } if (aRead<0.30 && ref<(2.6/3.3)) { ref=ref+0.0005; } if (ref>2.6/3.3) { ref=2.6/3.3; } if (ref<0) { ref=0; } i++; reference=ref; //sets the vReference aRead = reading.read(); //reads the new output aRead = aRead*3.3; //turns it into a real voltage. } //output is stable. float theValue=((2.6-(ref*3.3)))/0.040; value = theValue - 0.7; //there's an offset. if (theValue<0) { theValue = 0.0; } if (debug==1) { printf("VALUE: %.1f\n\r",theValue); }