2 years, 6 months ago.

my code did work now it wont, why?

Hey im new to mbed's and is just trying my luck with an analog temperature, lux and pH-sensor, the pH-sensors code was re-written from aduino which gave som problems, but they were solved. Now i tried to send it via wifly, it worked (on coolterm and the database) - then i changed something and then it didn't work anymore - not even the original, can someone tell me what i did wrong?

(sorry for a messy code)

#include "mbed.h" //mbeds library - gennem dette kan du benytte de funktioner mbeden har/kontrollere mbeden
#include <string> //
#include <sstream>
#include <algorithm> //To use string erase-remove

#define Offset -0.83         //deviation compensate
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40      //times of collection

DigitalOut led1(LED1), led2(LED2), led3(LED3),led4(LED4);

Serial wifly(p28,p27); //Tx,Rx (Wifi)
DigitalOut resetWifly(p26);

Serial pc(USBTX, USBRX);

AnalogIn measure1(p20); //temp 3.3V=1.0=65535, 0V=0.0=0
AnalogIn measure2(p19); //lux
AnalogIn measure3(p18); //ph

DigitalOut out0(p21); //ON = 3,3V, 40mA max for Solid State Relay  KUN HVIS JEG VIL HAVE EN TÆND/SLUKKNAP
DigitalIn in0(p22); //true = 3.3V. false = 0V

Timer millis; //Instead of millis() which returns the number of millicseconds since the program started

string data;
bool program_mode = false;

uint16_t digitalVol1 = 0;
uint16_t digitalVol2 = 0;
int pHArray[ArrayLenth];    //Store the average value of the sensor feedback
int pHArrayIndex=0;
float analogVol1 = 0;
float analogVol2 = 0;
double avergearray(int* arr, int number);
float resultTemp;
float resultLux;

void tickFunction() 
        static unsigned long samplingTime = millis.read_ms();
        static unsigned long printTime = millis.read_ms();
        static float pHValue,analogVol3;
        if(millis.read_ms()-samplingTime > samplingInterval)
            pHArray[pHArrayIndex++]=measure3.read_u16(); //samples between 0 and 65535 instead of 1024!
            analogVol3 = avergearray(pHArray, ArrayLenth)*3.3/65535;
            pHValue = 3.5*analogVol3+Offset;
        if(millis.read_ms() - printTime > printInterval) //Repeat over and over
            digitalVol1 = measure1.read_u16(); //indsæt temperaturmålinger
            analogVol1 = (3300.0/65535.0)*((float)digitalVol1); //omregn fra volt til faktisk værdi
            resultTemp = ((analogVol1-1406.34)/24.34);
            pc.printf("MiliVoltage: %.1f temperature:    %.1f C\r\n",analogVol1,resultTemp);//udskriver temperaturen på coolterm      %.1f float %d helt tal %s streng

            digitalVol2 = measure2.read_u16();
            analogVol2 = (3300.0/65535.0)*((float)digitalVol2);
            resultLux = pow((digitalVol2/12.304),1.556905);
            pc.printf("MiliVoltage:  %.1f lux:         %.1f\r\n",analogVol2,resultLux);
            pc.printf("Voltage:        %0.2f pH:             %0.2f\n\n", analogVol3,pHValue);

            int id = 1;
            char data4[10] = "OK";
            led1 = !led1;

Ticker tick;

int main() 
    resetWifly = 1; wait(1); resetWifly = 0; wait(1); resetWifly = 1; wait(1);      
    wait(5); while(wifly.readable()){wifly.getc(); wait(0.1);} //Clear wifly buffer
    tick.attach(&tickFunction, 1.0);
        if (wifly.readable()) 
            string message = "";
            char c ='0';
            Timer t;
            while(t.read_ms() < 200) 
                if (wifly.readable()) 
                    c = wifly.getc();
                    message += c;
                if (c == '\n') { break; }     
double avergearray(int* arr, int number)
    int i;
    int max,min;
    double avg;
    long amount=0;
        printf("Error number for the array to averaging!\n");
        return 0;
    if(number<5) //If data has been received from CoolTerm
        avg = amount/number;
        return avg;
            min = arr[0];max=arr[1];
                amount+=min;        //arr<min
                    amount+=max;    //arr>max
                    amount+=arr[i]; //min<=arr<=max
        avg = (double)amount/(number-2);
    return avg;

Ignore Karen's answer, posting the code like that is fine, it's far better that way than an image or screen shot. You can't copy and paste the code from an image.

The online editor does include a magic wand icon which will format the code for you. It's a good way of getting consistent indenting and catching missing brackets etc...

What do you mean it doesn't work - what part doesn't work? Do you get any errors?

A couple of basic tips, I doubt either of these are the cause of the problem but they are good habits to have:

1) Avoid things like printf and too much floating point maths inside an interrupt (your ticker function). They are slow and interrupts should be as fast as possible. printf can block until the buffer is free which can take a long time. A single float operation is 100+ clock cycles, a double is 200+ cycles, any decimal numbers are doubles unless you put an f on the end e.g. 10.0 is a double, 10.0f is a float. Instead read the ADC values into some variables and set a flag, the main loop can then look for that flag and processes the results. This way you aren't blocking all other interrupts for thousands of CPU cycles.

2) Avoid using the c++ string data type when possible for embedded systems. It makes life easier at times but it is also slower and uses more memory than using the c style char* method of storing a string.

Also line 129 needs a cast to a float or double or it will do an integer division before converting to a double.

posted by Andy A 02 Dec 2016
Comment on this question
Be the first to answer this question.

To post an answer, please log in.