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.
10 years, 4 months ago.
why I have problems with my program?
Hello everyone I wrote the following program, which displays the angle cams, depending on the pushbuttons. So when I press a button, a specific cam angle should be displayed depending on the button. But I have a problem. When compiling the program, only the angle of the push button number 1 is displayed. When I press a button the rest of the buttons, nothing happens. Can you please tell me, where does the error in my program? Thank you in advance.
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p6); // déclaration de p10 dans in
DigitalIn bougie(p7); //commande l'info bougie
DigitalIn rupt(p6);//Commande le rupteur
DigitalOut cc(p5);//commande le court-circuit; sert aussi à enlevé un des cylindres
// Déclaration des bp
DigitalIn bp1(p21);
DigitalIn bp2(p22);
DigitalIn bp3(p23);
DigitalIn bp4(p24);
DigitalIn bp5(p25);
DigitalIn bp6(p26);
// Déclaration des led
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
Timer t;
Timer t_on;
Timer t_off;
int t_period = 0; // This is the period between interrupts in microseconds
float t_freq = 0; // <-- Set in an interrupt but read in main. Should be volatile or the compiler could optimise it out.
void flip(); //Renvoie la frequence
int main() {
// déclaration des fonctions
int trmin;
int dwell=0;
//int ref_cyl2;
float alpha=0;
float angle1=0;float angle2=0;float angle3=0;float angle4=0;float angle5=0;float angle6=0;
in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); //Set up the interrupt for rising edge
t.start(); //start the timer
while (1) {
wait_ms(100);
trmin=(t_freq*4*30)/4;
if (in==1) { // <--- the input that you have created not the pin number.
t_on.start();
t_off.stop();
} else {
t_on.stop();
t_off.start();
alpha=t_on/(t_on+t_off);
dwell=alpha*100;
}
if(bp1==1) {
angle1=(dwell*360)/100;
lcd.printf("ac 1cy=%f\r\n",angle1);}
else if(bp2==1){
angle2=(dwell*360)/200;
lcd.printf("ac 2cy=%f\r\n",angle2);}
else if(bp3==1){
angle3=(dwell*360)/300;
lcd.printf("ac 3cy=%f\r\n",angle3);
else if(bp4==1){
angle4=(dwell*360)/400;
lcd.printf("ac 4cy=%f\r\n",angle4);}
else if(bp5==1){
angle5=(dwell*360)/500;
lcd.printf("ac 5cy=%f\r\n",angle5);}
else if(bp6==1){
angle6=(dwell*360)/600;
lcd.printf("ac 6cy=%f\r\n",angle6);}
}
}
void flip() {
t_period = t.read_us();// Get time since last interrupt
t_freq = (1/(float)t_period)*1000000; // Convert period (in us) to frequency (Hz)
t.reset(); // Reset timer and wait for next interrupt <--- Timing would be more accurate if this was one line earlier
}
2 Answers
10 years, 4 months ago.
How are the buttons wired. Are they always pulled to logic 0 by some resistor when no button is pressed and do they become logic high when you press a button? Note that typical input pins will be 1 when you dont connect anything to them. That might explain why angle1 is always displayed no matter which button is pressed.
Have you measured the voltage on the inputpin when the button is not pressed and when it is pressed? The 4 pin switches have two pairs of pins connected. See picture. You must use one of each pair. They dont work if they are rotated by 90 degrees.
.
10 years, 4 months ago.
This is crude, tested and runs but not sure if it will work correctly. Couldn't simulate as I do not know what your signal input parameters are.
Try this
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 //déclaration de p15,p16,P17... dans lcd
InterruptIn in(p6); // déclaration de p10 dans in
DigitalIn bougie(p7); //commande l'info bougie
DigitalIn rupt(p6); //Commande le rupteur
DigitalOut cc(p5); //commande le court-circuit; sert aussi à enlevé un des cylindres
// Déclaration des bp
DigitalIn bp1(p21);
DigitalIn bp2(p22);
DigitalIn bp3(p23);
DigitalIn bp4(p24);
DigitalIn bp5(p25);
DigitalIn bp6(p26);
// Déclaration des led
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
Timer t;
Timer t_on;
Timer t_off;
Ticker tick;
// déclaration des fonctions
//int ref_cyl2;
float trmin;
float dwell;
float alpha;
float angle1=0;float angle2=0;float angle3=0;float angle4=0;float angle5=0;float angle6=0;
float t_period; // This is the period between interrupts in microseconds
float t_freq; // <-- Set in an interrupt but read in main. Should be volatile or the compiler could optimise it out.
void flip() //Renvoie la frequence
{
t_period = t.read_us(); // Get time since last interrupt
t_freq = (1/t_period)*1000000; // Convert period (in us) to frequency (Hz)
t.reset(); // Reset timer and wait for next interrupt
}
int main() {
in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); //Set up the interrupt for rising edge
bp1.mode(PullDown);
bp2.mode(PullDown);
bp3.mode(PullDown);
bp4.mode(PullDown);
bp5.mode(PullDown);
bp6.mode(PullDown);
t.start(); //start the timer
lcd.printf("Press Switch 1-6");
lcd.printf("for cam angles");
while (1) {
wait_ms(100);
trmin=(t_freq*4*30)/4;
if (in==1){ // <--- the input that you have created not the pin number.
t_on.start();
t_off.stop();
}
else{
t_on.stop();
t_off.start();
alpha = t_on/(t_on+t_off);
dwell = alpha*100;
}
if(bp1==1) {
angle1=(dwell*360)/100;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 1cy =\n");
lcd.printf("%3.2f degres ",angle1);}
else if(bp2==1){
angle2=(dwell*360)/200;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 2cy =\n");
lcd.printf("%3.2f degres ",angle2);}
else if(bp3==1){
angle3=(dwell*360)/300;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 3cy =\n");
lcd.printf("%3.2f degres ",angle3);}
else if(bp4==1){
angle4=(dwell*360)/400;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 4cy =\n");
lcd.printf("%3.2f degres ",angle4);}
else if(bp5==1){
angle5=(dwell*360)/500;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 5cy =\n");
lcd.printf("%3.2f degres ",angle5);}
else if(bp6==1){
angle6=(dwell*360)/600;
lcd.cls();lcd.locate(0,0);
lcd.printf("ac 6cy =\n");
lcd.printf("%3.2f degres ",angle6);}
}
}
Carefully take a wire with one end connected to the 3.3v Vout (pin40 on the Mbed) then touch the other end to each of the DigitalIn pins one at a time. you should then see the display change to relevant angles.
If that works you can then add six separate switches to each of the bp inputs. one switch connection to 3.3v Vout the other connection to the bp Digital In pin of the Mbed. Take a look at Wim's drawings.
I try this programm but it doesn't work. Just the buttons 6 and 5 are working
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p6);// déclaration de p10 dans in
DigitalIn bougie(p7); //commande l'info bougie
DigitalIn rupt(p6);//Commande le rupteur
DigitalOut cc(p5);//commande le court-circuit; sert aussi à enlevé un des cylindres
// Déclaration des bp
DigitalIn bp1(p21);
DigitalIn bp2(p22);
DigitalIn bp3(p23);
DigitalIn bp4(p24);
DigitalIn bp5(p25);
DigitalIn bp6(p26);
// Déclaration des led
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
Timer t;
Timer t_on;
Timer t_off;
int t_period = 0; // This is the period between interrupts in microseconds
float t_freq = 0; // <-- Set in an interrupt but read in main. Should be volatile or the compiler could optimise it out.
void flip(); //Renvoie la frequence
int main() {
// déclaration des fonctions
int trmin;
int dwell=0;
//int ref_cyl2;
float alpha=0;
float angle1=0;float angle2=0;float angle3=0;float angle4=0;float angle5=0;float angle6=0;
in.mode(PullDown); // Set the pin to Pull Down mode.
bp1.mode(PullDown);
bp2.mode(PullDown);
bp3.mode(PullDown);
bp4.mode(PullDown);
bp5.mode(PullDown);
bp6.mode(PullDown);
in.rise(&flip); //Set up the interrupt for rising edge
t.start(); //start the timer
while (1) {
wait_ms(100);
trmin=(t_freq*4*30)/4;
if (in==1) { // <--- the input that you have created not the pin number.
t_on.start();
t_off.stop();
} else {
t_on.stop();
t_off.start();
alpha=t_on/(t_on+t_off);
dwell=alpha*100;
}
if(bp1==1) {
angle1=(dwell*360)/100;
lcd.printf("ac 1cy=%f\r\n",angle1);}
if(bp2==1){
angle2=(dwell*360)/200;
lcd.printf("ac 2cy=%f\r\n",angle2);}
if(bp3==1){
angle3=(dwell*360)/300;
lcd.printf("ac 3cy=%f\r\n",angle3);}
if(bp4==1){
angle4=(dwell*360)/400;
lcd.printf("ac 4cy=%f\r\n",angle4);}
if(bp5==1){
angle5=(dwell*360)/500;
lcd.printf("ac 5cy=%f\r\n",angle5);}
if(bp6==1){
angle6=(dwell*360)/600;
lcd.printf("ac 6cy=%f\r\n",angle6);}
}
}
void flip() {
t_period = t.read_us();// Get time since last interrupt
t_freq = (1/(float)t_period)*1000000; // Convert period (in us) to frequency (Hz)
t.reset(); // Reset timer and wait for next interrupt <--- Timing would be more accurate if this was one line earlier
}
When button 5 and 6 are working and the others dont you should check the wiring of the failing buttons. Try connecting button 5 or 6 to the other input pins to see if they respond also.
posted by 20 Jun 2015His code didn't work Wim, I quickly re jigged and tried it and now works, but I'm not sure what his input parameters are also there are some extras like 'Ticker tick' that possibly needs to be incorporated at some stage, so I could not conclusively test. However he can now at least see the display working and changing to each of the switch inputs does give an expected reading on the display.
posted by 20 Jun 2015