If Statement Not Recognizing Value of the Variable

So I’m trying to learn to program in pure C and have an exercise from a book I’m using that asks me to connect to a 7 segment display using 7 digital pins. For this problem, there are a few exceptions in the use of Arduino functions to ease the learning process. I am then supposed to have it take in a character from the serial monitor and print it out onto the display.

The problem is that I can get the program to print back the character to confirm it’s receiving the variable, but the value doesn’t get recognized by the if statement, or the if statement isn’t getting executed. What am I doing wrong here?

2c.ino (1.62 KB)

Remember that = is different to ==.

What is sending the serial data? If it is the Serial Monitor, you are NOT receiving a 1. You are receiving a '1'. NOT the same thing at all.

Your MyDelay() function is silly,

PaulS:
What is sending the serial data? If it is the Serial Monitor, you are NOT receiving a 1. You are receiving a ‘1’. NOT the same thing at all.

Your MyDelay() function is silly,

ok so, my input is a character (which i knew), but my the value in the if statement that letnum is being compared to is an integer? I’ll try adjusting that tonight!

As for the MyDelay(), it was a previous exercise that I carried over… Trying to do as much pure C as i can before including any libraries or arduino functions.

In all likelihood your MyDelay function is being optimized out as not doing anything. As the loop is now not pausing for 1000 whatevers, you are looping straight back to another read where the serial buffer is probably empty (because it can't fill that fast) and letnum is forever null. The print statements will not work either as the loop is happening a light speed and serial output is at a 9600 baud crawl.

As far as pure C is concerned, does that mean you cant use any GCC libraries? You could add util/delay.h and employ _delay_ms(1000) to get your delay. Or do a volatile inline asm loop with a few million nop. There are several tutorials on using the asm/nop stub function for delays. Set one up for say 10 microseconds and the just call it in loops. Make sure it carries a volatile attribute so the compiler leaves it alone.

In all likelihood your MyDelay function is being optimized out as not doing anything. As the loop is now not pausing for 1000 whatevers

It is not, because the index is declared volatile. The compiler has no way of determining that the variable by have it's value changed outside of the MyDelay() function, so it can't optimize away the for loop.

However, it takes VERY little time to execute the increment and comparison statements, so it seems pointless.

Getting rid of ALL delaying tactics, as illustrated in the blink without delay example, and countless others, is a far better thing to do.