3 years, 3 months ago.

Surely this is simple?

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.

Hi Myles,

Have you gotten any further with this issue yet? The most immediate thing I can see wrong with your program is that it'll only run once so you'll have a matter of nanoseconds to apply stimulus to that input. If you wrap the body of main() in a while(1){ } then the program will check for the input every couple of nanosceonds - so you'll have a much wider chance of seeing anything happening!

posted by Russell Bateman 03 Nov 2017
Be the first to answer this question.