EEVblog Electronics Community Forum
Electronics => Beginners => Topic started by: skillz21 on October 15, 2016, 11:06:33 pm
-
Hi guys,
I made a circuit with a (HC-SR04) and an Arduino. It displays the distance on a seven segment display. Then i decided to add a MQ-2 Gas sensor on an well. I added a button so that i can cycle between two options. It -you guessed it!- doesn't work :palm:. :scared: :scared:. Anyway, i have the code attached. So, where did i go wrong? and how can i fix it?
Thanks!
-
Things like this just eat C programmers:
if(cycleState = 3){
I didn't look any further.
You clearly wanted if (cycleState == 3) {
Might I suggest that you always turn the expression around?
if (3 == cycleState) <= this will compile as long as it is a comparison
whereas
if (3 = cycleState) <= this will not compile
Do this religiously and if you make a mistake the compiler will remind you.
-
Get a good C or C++ book and go through it, doing all the exercises.
For your code, start with checking the difference between assignment and equality comparison.
-
oh... im so sorry. i didnt notice that... sorry guys, im still pretty new to this ^-^
-
Get a good C or C++ book and go through it, doing all the exercises.
For your code, start with checking the difference between assignment and equality comparison.
is there any free ebooks that you know?
-
For C: 'The C Book' (http://publications.gbdirect.co.uk/c_book/) (2nd ed.) by Mike Banahan, Declan Brady and Mark Doran. Its not as good as K&R but its free and legal.
Its C89 (https://en.wikipedia.org/wiki/ANSI_C) so you wont see stuff like declaring the loop variable inside the for() statement, but it will serve you well enough for the basics.
Hint: look at the switch(expression) ... case const
: (http://publications.gbdirect.co.uk/c_book/chapter3/flow_control.html#section-5) flow control structure - its much better than a bunch of if()s testing the same variable is equal to various constants
-
oh... im so sorry. i didnt notice that... sorry guys, im still pretty new to this ^-^
How do you think I picked it out so quickly? Been there, done that, still have the scars.
-
I just picked upp the 2d Edition "The C Programming Language" (K&R) and the matching "The C Answer Book" combined for about $12 at Alibris. Both books are marked "Circulation of this edition outside the Indian subcontinent is UNAUTHORIZED"
So how come the same publisher, Prentice Hall, can publish the book, written in the US, for the Indian market and sell it dirt cheap while charging me full price ($93) at Amazon) in the US?
The first edition is still a fine book. It just doesn't fully comply with 'ANSI C'.
Ultimately, both books came from Bookloads in Santa Clara. Apparently, they have more...
The nice thing about K&R is that their examples have real value. I use their string functions in every embedded project I create.