Cycling through different values on a display - Page 1
Forum section: electronics / beginners

author: skillz21
author_rank: Frequent Contributor
timestamp: October 15, 2016, 11:06:33 pm
content: 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.. Anyway, i have the code attached. So, where did i go wrong? and how can i fix it?Thanks!

author: rstofer
author_rank: Super Contributor
timestamp: October 16, 2016, 01:19:04 am
content: 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 comparisonwhereasif (3 = cycleState) <= this will not compileDo this religiously and if you make a mistake the compiler will remind you.

author: newbrain
author_rank: Super Contributor
timestamp: October 16, 2016, 01:24:42 am
content: 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.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 16, 2016, 01:25:29 am
content: oh... im so sorry. i didnt notice that... sorry guys, im still pretty new to this

author: skillz21
author_rank: Frequent Contributor
timestamp: October 16, 2016, 01:26:36 am
content: Quote from: newbrain on October 16, 2016, 01:24:42 amis there any free ebooks that you know?

author: Ian.M
author_rank: Super Contributor
timestamp: October 16, 2016, 01:42:28 am
content: For C:'The C Book'(2nd ed.) by Mike Banahan, Declan Brady and Mark Doran. Its not as good as K&R but its free and legal.ItsC89so 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 theswitch(expression) ... caseconst:flow control structure - its much better than a bunch of if()s testing the same variable is equal to various constants

author: rstofer
author_rank: Super Contributor
timestamp: October 16, 2016, 02:12:36 am
content: Quote from: skillz21 on October 16, 2016, 01:25:29 amHow do you think I picked it out so quickly? Been there, done that, still have the scars.

author: rstofer
author_rank: Super Contributor
timestamp: October 16, 2016, 02:36:07 am
content: 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.