Author Topic: Cycling through different values on a display  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Cycling through different values on a display
« 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!
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9929
  • Country: us
Re: Cycling through different values on a display
« Reply #1 on: October 16, 2016, 01:19:04 am »
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.
« Last Edit: October 16, 2016, 01:22:17 am by rstofer »
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1761
  • Country: se
Re: Cycling through different values on a display
« Reply #2 on: October 16, 2016, 01:24:42 am »
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.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Cycling through different values on a display
« Reply #3 on: October 16, 2016, 01:25:29 am »
oh... im so sorry. i didnt notice that... sorry guys, im still pretty new to this ^-^
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Cycling through different values on a display
« Reply #4 on: October 16, 2016, 01:26:36 am »
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?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13026
Re: Cycling through different values on a display
« Reply #5 on: October 16, 2016, 01:42:28 am »
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.

Its C89 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
:
flow control structure - its much better than a bunch of if()s testing the same variable is equal to various constants
« Last Edit: October 16, 2016, 02:44:08 am by Ian.M »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9929
  • Country: us
Re: Cycling through different values on a display
« Reply #6 on: October 16, 2016, 02:12:36 am »
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.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9929
  • Country: us
Re: Cycling through different values on a display
« Reply #7 on: October 16, 2016, 02:36:07 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf