First program newbie question

23 Aug 2012

My first simple project failed, Can someone help me understand why this while(1) statment stops my program in its tracks? C is pretty new to me.. Thanks in advanced

#include "mbed.h"
#include "Terminal.h"

Terminal term(USBTX, USBRX); // tx, rx
AnalogIn input1(p15);
AnalogIn input2(p16);
AnalogIn input3(p17);
AnalogIn input4(p18);
AnalogIn input5(p19);
AnalogIn input6(p20);
unsigned short ADCRAW1;  
unsigned short ADCRAW2; 
unsigned short ADCRAW3; 
unsigned short ADCRAW4; 
unsigned short ADCRAW5; 
unsigned short ADCRAW6;  

int main() {
    while(1) {// works if I dont use this line
        term.cls();
        term.printf("        6 Channel voltmeter");  //Only gets to this line
        term.locate(1,2);
        term.printf("Channel 1= ");
        term.locate(30,2);
        term.printf("Channel 2= ");
        term.locate(1,4);
        term.printf("Channel 3= ");
        term.locate(30,4);
        term.printf("Channel 4= ");
        term.locate(1,6);
        term.printf("Channel 5= ");
        term.locate(30,6);
        term.printf("Channel 6= ");
    
        ADCRAW1 = input1.read_u16();
        wait_ms(1);
        ADCRAW2 = input2.read_u16();
        wait_ms(1);
        ADCRAW3 = input3.read_u16();
        wait_ms(1);
        ADCRAW4 = input4.read_u16();
        wait_ms(1);
        ADCRAW5 = input5.read_u16();
        wait_ms(1);
        ADCRAW6 = input6.read_u16();
        wait_ms(1);
        
        term.locate(15,2);
        term.printf("%d", ADCRAW1);
        term.locate(45,2);
        term.printf("%d", ADCRAW3);
        term.locate(15,4);
        term.printf("%d", ADCRAW3);
        term.locate(45,4);
        term.printf("%d", ADCRAW4);
        term.locate(15,6);
        term.printf("%d", ADCRAW5);
        term.locate(45,6);
        term.printf("%d", ADCRAW6);
        wait(1);
    }
}
23 Aug 2012

Copying your code and launching teraterm gives me as result:

/media/uploads/Sissors/mbed.png

AD pins are unconnected, so gives random noise back, but seems to be working fine for me. When running the code, does the LED on the mbed flash? (The one near the USB connection). If yes, then the problem is not in your mbed program but in your terminal program. You can also try another USB cable maybe.

23 Aug 2012

Hi Erik, Thanks for your reply. Without the while(1) statement the program works fine but when its anywhere in the program it only displays to the line, 6 Channel volt meter. I have now substituted it "for (int i = 0; i <=2; i++){ " then I insert a "i dash dash" at the end to cheat the same function to make it work but its really weird. This code works

#include "mbed.h"
#include "Terminal.h"

Terminal term(USBTX, USBRX); // tx, rx
AnalogIn input1(p15);
AnalogIn input2(p16);
AnalogIn input3(p17);
AnalogOut signal(p18);
AnalogIn input5(p19);
AnalogIn input6(p20);
DigitalOut testled(LED1);


  

int main() {
    
        signal = 0.25;      //determins the voltage out signal
        term.cls();
        term.printf("        5 Channel voltmeter 1 Generator");  //Only gets to this line
        term.locate(1,2);
        term.printf("Channel 1= ");
        term.locate(30,2);
        term.printf("Channel 2= ");
        term.locate(1,4);
        term.printf("Channel 3= ");
        term.locate(30,4);
        term.printf("Signal  4= ");
        term.locate(1,6);
        term.printf("Channel 5= ");
        term.locate(30,6);
        term.printf("Channel 6= ");
        
        for (int i = 0; i <=2; i++){ 
        float voltage1 = input1*3.3;
        float voltage2 = input2*3.3;
        float voltage3 = input3*3.3;
        float signal4 = signal*3.3; // this is my output
        float voltage5 = input5*3.3;
        float voltage6 = input6*3.3;
        
        term.locate(15,2);
        term.printf("%.3f Vdc",voltage1);
        term.locate(45,2);
        term.printf("%.3f Vdc",voltage2);
        term.locate(15,4);
        term.printf("%.3f Vdc",voltage3);
        term.locate(45,4);
        term.printf("%.3f Vdc",signal4);
        term.locate(15,6);
        term.printf("%.3f Vdc",voltage5);
        term.locate(45,6);
        term.printf("%.3f Vdc",voltage6);
        wait_ms(100);
        i--;
        }
        
    

        
        
    
}

This code does not, The LED is solid..

#include "mbed.h"
#include "Terminal.h"

Terminal term(USBTX, USBRX); // tx, rx
AnalogIn input1(p15);
AnalogIn input2(p16);
AnalogIn input3(p17);
AnalogOut signal(p18);
AnalogIn input5(p19);
AnalogIn input6(p20);
DigitalOut testled(LED1);


  

int main() {
    
        signal = 0.25;      //determins the voltage out signal
        term.cls();
        term.printf("        5 Channel voltmeter 1 Generator");  //Only gets to this line
        term.locate(1,2);
        term.printf("Channel 1= ");
        term.locate(30,2);
        term.printf("Channel 2= ");
        term.locate(1,4);
        term.printf("Channel 3= ");
        term.locate(30,4);
        term.printf("Signal  4= ");
        term.locate(1,6);
        term.printf("Channel 5= ");
        term.locate(30,6);
        term.printf("Channel 6= ");
        
        //for (int i = 0; i <=2; i++){ 
        while(1){
        float voltage1 = input1*3.3;
        float voltage2 = input2*3.3;
        float voltage3 = input3*3.3;
        float signal4 = signal*3.3; // this is my output
        float voltage5 = input5*3.3;
        float voltage6 = input6*3.3;
        
        term.locate(15,2);
        term.printf("%.3f Vdc",voltage1);
        term.locate(45,2);
        term.printf("%.3f Vdc",voltage2);
        term.locate(15,4);
        term.printf("%.3f Vdc",voltage3);
        term.locate(45,4);
        term.printf("%.3f Vdc",signal4);
        term.locate(15,6);
        term.printf("%.3f Vdc",voltage5);
        term.locate(45,6);
        term.printf("%.3f Vdc",voltage6);
        wait_ms(100);
        //i--;
        }
        
    

        
        
    
}
23 Aug 2012

Perhaps the problem is related to error in the locate method of terminal.cpp as reported by Aaryn Smith. See here. Maybe this problem occurs in combination with the PC terminal software that you use.

Try inserting the terminal source code in your program and then fixing the locate problem as proposed by Aaryn.

24 Aug 2012

Wim Huiskamp wrote:

Perhaps the problem is related to error in the locate method of terminal.cpp as reported by Aaryn Smith. See here. Maybe this problem occurs in combination with the PC terminal software that you use.

Try inserting the terminal source code in your program and then fixing the locate problem as proposed by Aaryn.

Hi Wim, This did not fix the problem either.

24 Aug 2012

I would try removing the locate code completely, to see if it does start working. Although I still consider it weird that it does run fine on my computer.

24 Aug 2012

Erik - wrote:

I would try removing the locate code completely, to see if it does start working. Although I still consider it weird that it does run fine on my computer.

I cant explain this but now it is working... Yesterday I tried the above code on a PC and a MAC and now its working. I hope my further learning of the C langage and M-Bed devices don't run into any similar issues. It started working after I used the "Serial pc(USBTX, USBRX); tx, rx" commands within the same program but now if I take them back out of the program to the original code it still works.