6 years, 6 months ago.

Complete beginner question!

Hi everyone. So, I am a COMPLETE and utter novice at programming. I have done some basic stuff on Arduinos (literally toggling LEDs and displaying something on an LCD) and I am trying to self teach myself how to program in C. I am a hardware engineer by trade, but it bothers me that I can't do any of the firmware/software side and there are no evening courses to teach it, and I'd like to further my career options. I am struggling to understand how some of these commands go together and have run into an issue that I just can;t get my head around why it isn't working.

So, I have an input and an output. My output is toggling the gate of a FET which turns and LED on. The input is coming from an AND gate. So, my LED is always on, and when I get an input signal from the AND gate (2 conditions have been met) I want the output (LED toggle) to go LOW (turn off the LED. As the output is also connected to one of the AND inputs, this will also turn the input signal LOW.

What I want to do: I just want to read the input as 'conditions met' and turn the LED off. It should then be off for 1 second, and turn back on. If the input goes HIGH again, the process repeats. I am using a simple push to make switch as the other AND gate input and have measured that the output (MCU input) goes high when the button is pressed, yet the LED toggle (output) will not turn off. My code is (I think) pretty damn simple, but clearly I don't understand something correctly as it just isn't working.

So this is the code I am using:

  1. include "mbed.h"

DigitalIn ip(D7); DigitalOut op(D8);

int main() { if (ip == 1){ op = 0; wait (1.0); op = 1;} else { op = 1;} }

And to me, that seems logical. In the usual state, the output is HIGH. If the input gets the signal from the AND gate, the LED will turn off for 1 second, then turn on again.

What is it I've done wrong as that looks like the logical way to do it and I just can't understand why that doesn't work?

If it helps, I am using the Nucleo F103RB. When I use the 'blink' code and just toggle the LED on and off like that, it works fine, it's just when I add the 'if' statement that it goes wrong.

2 Answers

6 years, 5 months ago.

hi Myles,

First, for easier reading of your code, please click on the "Editing tips" in the bottom right corner of the comment box. Then you can see how to wrap your code with tags to make it easier to read.

include "mbed.h"
DigitalIn ip(D7); 
DigitalOut op(D8);

int main() { 
    if (ip == 1) { 
       op = 0; 
       wait (1.0); 
       op = 1;
    } else { 
       op = 1;
    }
}

Now, with that out of the way, your code looks fine, except for perhaps 1 thing - it will execute only once. You probably want this to happen each time you push the button, so you need to put your code in a "forever" loop.

There are a few common ways to implement the forever loop:

    for (;;) {
        // inner loop here
    }

    while (1) {
        // inner loop here
    }

So, put your code starting with your "if" test in that inner loop. If you see the "blinky" example code, you'll see examples of both the forever loop, timer and led control.

6 years, 5 months ago.

Myles -

As David noted above, your program does not loop. It executes the if/else code only once.

The set of nine presentations in the online course notes in the Documentation > Cookbook section is a good way to jump start your mbed efforts.

https://os.mbed.com/cookbook/Course-Notes

The examples use the LPC 1768, but you should have no problem modifying them as needed for your Nucleo board.

The latest (2016) version of the companion book "Fast and Effective Embedded Systems Design: Applying the ARM mbed" has a number of useful updates, and you will find a lot of good tutorial information at the authors’ website.