Shop OBEX P1 Docs P2 Docs Learn Events
First C: C on the Propeller for Beginners — Parallax Forums

First C: C on the Propeller for Beginners

TymkrsTymkrs Posts: 539
edited 2013-11-21 05:36 in Learn with BlocklyProp
So some of you may be familiar with First Spin, our podcast about learning how to program in Spin on the Parallax Propeller from zero programming experience. If you haven't had an opportunity to check it out, anytime is a good time!

But this post is to introduce folks to First C! It's a podcast we just started about how to program in C with little programming experience - or maybe only Spin experience. We approach it step by step and go through some of the tutorials provided by Parallax while adding in some extra tips.

First Episode: http://www.tymkrs.com/shows/episode/first-c-episode-001/
Second Episode: http://www.tymkrs.com/shows/episode/firstc-episode-002/
Third Episode: http://www.tymkrs.com/shows/episode/firstc-episode-003/

T
his short series will be posted weekly on Tuesdays on http://tymkrs.com/shows and we welcome any questions!
«1

Comments

  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-31 17:24
    Do you by chance have anything yet for programming Parallax's (or other) IR Receivers with SimpleIDE? I have code for the Ping and my Servos but I need an example for IR. Thanks.
  • TymkrsTymkrs Posts: 539
    edited 2013-07-31 23:35
    NWCCTV wrote: »
    Do you by chance have anything yet for programming Parallax's (or other) IR Receivers with SimpleIDE? I have code for the Ping and my Servos but I need an example for IR. Thanks.

    Per Roy: "Reading an IR sensor is just reading a pin and tracking the state over time. You don't need anything special."

    If I hear of anyone who has code available to share, I'll let you know :)

    As reference, here's the Spin version - may be easily ported to C? http://learn.parallax.com/ir-remote-propeller-spin-demo
  • TymkrsTymkrs Posts: 539
    edited 2013-08-01 00:01
    So Roy gave me a little more information about IR sensors that may help:

    The way remotes work is that, with each button press, they send a set of pulses with varying length in the low state of the pulse. So a long low state = 1, and a short low state = 0.

    So first tyou wait for the pin to go HIGH, then you wait for it to go low and count the time that it is low. If the time it is low is longer than a threshold amount (in the spin example above it is 25 iterations of a spin loop reading the pin state), then it is a 1. If its low is shorter than that, then it's a 0.

    You track bits until the code is done, shift them into a long, then that number is your code for what button was pressed on the remote. For example, this could be what is seen from a single button press:
    ir_signal_drawing.gif

    You can make a program to just track the pin state and tell you the length of time it stays high and low, and then aim a remote at it and press buttons. And it'll tell you the lengths of the high and low stats. The sequence of highs/lows will be different for each button, but the timing of low and high pulses will be consistent.

    Don't know if this helps?
    720 x 540 - 13K
  • jazzedjazzed Posts: 11,803
    edited 2013-08-01 10:48
    Nice description.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-08-01 20:16
    Very nice indeed. However, my programming skills are basically low. If I see a short demo I can take that and run with it but I normally need a starting point. I have the Spin code so will look at it and see if there is a way to figure it out in SimpleIDE. I really like SimpleIDE and can't wait for more project files to learn and copy from. I have completed the entire SimpleIDE lessons and am anxiously awaiting more.
  • jazzedjazzed Posts: 11,803
    edited 2013-08-01 23:17
    Priority now is pushing as hard as possible to get one last package out before release.
    I've queued up some keyboard and VGAtext stuff and will be doing TVtext soon.

    I write all programs with SimpleIDE. Gotta eat that dog food as some say ;-)
  • TymkrsTymkrs Posts: 539
    edited 2013-08-06 16:39
    Welcome to the fourth episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week we go through some experimental floating point math code Addie wrote, and talk about boolean logic involved in decision making, IF, ELSEIF, ELSE statements, and how C approaches conditional statements. We also talk a tiny bit about switch statements and cast statements!

    Episode Link:
    http://www.tymkrs.com/shows/episode/firstc-episode-004/

    Code Links:
    http://learn.parallax.com/propeller-c-start-simple/make-several-decisions
    http://learn.parallax.com/propeller-c-start-simple/make-complicated-decisions
    http://learn.parallax.com/propeller-c-start-simple/code-repeats
    http://tymkrs.tumblr.com/post/56977491012/array-variable-experimentation-in-c-on-propeller
    http://tymkrs.tumblr.com/post/57070755682/more-array-variable-experimentation
  • TymkrsTymkrs Posts: 539
    edited 2013-08-13 17:02
    Welcome to the fifth episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week, we look again at case uses of While, For, Sizeof, and examine how complex these instructions can go. We go through some counter examples, and tie up loose ends with index array variables!

    Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-005/



    Links:

  • TymkrsTymkrs Posts: 539
    edited 2013-08-20 16:10
    Welcome to the sixth episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week, we go hardware style! We talk about how to blink leds, how to blink multiple leds, how to use a pushbutton, and how to make sounds happen through piezo buzzers! It’s nice to get back to a little hardware and the basics of blinky LEDs, my favorite!

    Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-006/

    Links:
  • TymkrsTymkrs Posts: 539
    edited 2013-08-27 16:59
    Welcome to the seventh episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week, we go hardware style again! We talk about the ADCs on the Activity Board and measuring voltage with simple code. We cover the "Set Volt" lesson, which is a great way to figure out how much voltage to send to an LED on a normal project, and even touch on the Propeller 2! Can it be true?!

    Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-007/



    Temperature + Voltage: http://pastebin.com/5NeN8HTg
    LED Brightness + Voltage: http://pastebin.com/a2k5Tcc5
    Railroad Sign: http://pastebin.com/BiPj5UsB
  • dgatelydgately Posts: 1,631
    edited 2013-08-27 20:17
    Tymkrs wrote: »
    Welcome to the seventh episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week, we go hardware style again! We talk about the ADCs on the Activity Board and measuring voltage with simple code. We cover the "Set Volt" lesson, which is a great way to figure out how much voltage to send to an LED on a normal project, and even touch on the Propeller 2! Can it be true?!

    Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-007/



    Temperature + Voltage: http://pastebin.com/5NeN8HTg
    LED Brightness + Voltage: http://pastebin.com/a2k5Tcc5
    Railroad Sign: http://pastebin.com/BiPj5UsB

    Example code for a2k5Tcc5.c has a bug or two...
    [LIST=1]
    [*][COLOR=#000000]  pause(2000);[/COLOR]
    [*][COLOR=#000000]  while(1)[/COLOR]
    [*][COLOR=#000000]   {[/COLOR]
    [*][COLOR=#000000]     for(int v = 0; v < 256; v++)S         <== 1. The "S" is probably a typo and cause compile errors (Roy mentions in the pod cast)[/COLOR]
    [*][COLOR=#000000]     {                                     <== 2. You need to remove the declaration of v within the for loop or 
                                               <==    set the -std=c99 option [/COLOR]
    //   a2k5Tcc5.c:30:6: error: 'for' loop initial declarations are only allowed in C99 mode
    //   a2k5Tcc5.c:30:6: note: use option -std=c99 or -std=gnu99 to compile your code
    [*][COLOR=#000000]       dac_ctr(26, 0, v);[/COLOR]
    [*][COLOR=#000000]       dac_ctr(27, 1, 255-v);[/COLOR]
    [*][COLOR=#000000]       pause(4);[/COLOR]
    [*][COLOR=#000000]     }[/COLOR]
    [*][COLOR=#000000]   }[/COLOR]
    [*][COLOR=#000000]}[/COLOR]
    [/LIST]
    

    Example code should probably not depend on "-std=c99" options since most new users will not know about this...


    dgately
  • tdlivingstdlivings Posts: 437
    edited 2013-08-28 08:13
    dgately wrote: »
    Example code for a2k5Tcc5.c has a bug or two...


    Example code should probably not depend on "-std=c99" options since most new users will not know about this...


    dgately

    Just one man's opinion I see nothing wrong with -std=c99 and for(int v = 0; v < 256; v++)
    1999 was a long time ago and we do not need to remain in prehistoric C times
    SimpleIDE comes set up for c99
    New users will not have an issue with it, I am sure they can handle being told
    it is c99. Because the greatest lesson for new programmers is that nothing ever
    is exactly the standard and to be prepared for that because it is always evolving.

    Tom


  • Dave HeinDave Hein Posts: 6,347
    edited 2013-08-28 10:18
    Declaring the loop variable within the for statement is not standard practice in C. In my opinion, it would be better to teach new programmers to define all their variables at the beginning of the function. Later on, they can be taught that that they can define variables immediately after a "{" character, and it's scope is within that section. Then they could be shown the C99 "trick" of defining the loop variable in the for statement. Just my 2 cents worth from someone who learned C in the previous millennium.
  • dgatelydgately Posts: 1,631
    edited 2013-08-28 10:33
    tdlivings wrote: »
    Just one man's opinion I see nothing wrong with -std=c99 and for(int v = 0; v < 256; v++)
    1999 was a long time ago and we do not need to remain in prehistoric C times
    SimpleIDE comes set up for c99
    New users will not have an issue with it, I am sure they can handle being told
    it is c99. Because the greatest lesson for new programmers is that nothing ever
    is exactly the standard and to be prepared for that because it is always evolving.

    I think you missed the point... My default install of propgcc and SimpleIDE was not configured for c99 (possibly a difference between Win and Mac OS X installs?). So, if that's true for other new C users, they may not know that "-std-c99" would need to be added to compiler options! Not a matter of right or wrong, more a matter of how to help an unfamiliar user.


    dgately
  • TymkrsTymkrs Posts: 539
    edited 2013-08-28 11:15
    Oh hah, yes I probably should have taken out that S - it was when my fingers defaulted to cntrl-s to save the program instead of cntrl-shift-s. Um, that being said, the code isn't meant to be used as is, it's my experimentation with seeing what can be done based on the tutorials and challenges but also my own brain's experimentation. Most of which is pretty out there :p

    Um, and then this is the first time I've ever heard of -std/c99. I'm sure that's something that we can touch on in next week's chat, but I'm going to assume from context that -std/99 is what allows you to define a variable after {, whereas before, you had to define variables before the functions (which is good form, I agree).

    What I think is interesting, but anticipated, was that listeners who knew how to code in C would be OMG-ing over my noob code! I welcome suggestions for the direction we should take to help other newbies.

    Addie
  • jazzedjazzed Posts: 11,803
    edited 2013-08-28 12:04
    Tymkrs, CTRL+S will save just the file you are editing (CMD+S for Mac).


    Regarding --std=c99

    GCC allows specifying the "C standard vintage" to use. --std=c99 is (usually) set on the Learn examples and the Welcome project.

    The description of "Language Standards Supported by GCC" for Propeller-GCC can be found here: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc.pdf

    Propeller-GCC is based on the 4.6.1 revision of GCC, so -std=c11 is not supported yet.

    Clicking "New Project" should set --std=c99 by default. New Project for Simple View should also set 32-bit doubles, but Project View does not at the moment (can be irritating).

    The whole idea of standards can be confusing and irritating. Parallax Education chose to explicitly use GCC --std=c99 most likely to say "hey, we are using this" to the Professor and not necessarily the student.
  • TymkrsTymkrs Posts: 539
    edited 2013-08-28 12:16
    jazzed wrote: »
    Tymkrs, CTRL+S will save just the file you are editing (CMD+S for Mac).

    I thought it would, but for some reason I've had some real issues with saving my files such that pulling them up later = strange state of affairs (half edited code, or original code, or original code wiped by current code). I'll see if I can replicate the issue I've had, and get back to you on it.

    I am also in Simple View, no idea if that changes things
  • jazzedjazzed Posts: 11,803
    edited 2013-08-28 12:58
    Provide a step by step list.

    #1: Use Simple View.
    #2: Other steps to recreate the problem.

    Thanks,
    --Steve
  • dgatelydgately Posts: 1,631
    edited 2013-08-28 15:13
    Tymkrs wrote: »
    Um, that being said, the code isn't meant to be used as is, it's my experimentation with seeing what can be done based on the tutorials and challenges but also my own brain's experimentation. Most of which is pretty out there :p

    Sorry, should I not have brought up the issue? :innocent:

    Anyway, I just thought of the "newbie" C users and how they might react to compiling example code. It could be very frustrating if the example code they are learning from does not compile without edit.


    dgately
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2013-08-28 15:57
    We'll need to mention the C99 flag in the next episode. Thanks for bringing it up.

    I think it's silly to be debating using standard compliant code for the 14 year old standard. C99 is perfectly fine to assume and teach.

    I personally think it's better and more clear to have variable declaration closer to usage instead of only at the top of the function. I think it's silly to teach people the "old way" that is less desirable.

    Of course, if it was up to me, it would all be C++ instead of C. ;)
  • TymkrsTymkrs Posts: 539
    edited 2013-08-28 17:22
    Well, I decided to do a fresh install of the latest version of SimpleIDE - of course testing the save thing didn't show me any issues, but I'll keep an eye out for it. In the mean time however, I've run the following code:

    http://pastebin.com/Ng4nNGBM

    And have gotten the following error:


    In function '_main':
    (.text+0x0): multiple definition of '_main'
    cmm/Seven Segment Test TWo.o:(.text+0x0): first defined here
    collect2: ld returned 1 exit status
    Done. Build Failed!

    It was suggested that I add -lsimpletools to "Other Linker Options" in the Linker tab, but that didn't change the error. The line at fault that's highlighted when errors come up is whatever line my cursor happens to be on.

    I asked the tymkrs IRC and longhornengineer said he was able to compile it with no errors. Any suggestions?

    Addie
  • TymkrsTymkrs Posts: 539
    edited 2013-08-28 17:45
    Oh hah. Noobish delight! So I, at some point, added a tab so it'd be easier for me to see the code I was coming from and the code I was going to. This caused multiple files to be in my "Test 3" version of things:

    2h58od2.png


    So first you'll notice I added the -lsimpletools, but then I also deleted Test two - and when I ran it, it compiled! huzzah! lessons learned! And thanks to lostcauz/tautic/longhorn for their genius :)
    948 x 929 - 44K
  • jazzedjazzed Posts: 11,803
    edited 2013-08-29 07:03
    Tymkrs wrote: »

    So first you'll notice I added the -lsimpletools

    You don't need that as long as Simple Libraries are used, and Properties -> General Tab -> Auto Include Libraries is checked.

    (The Simple Library alternative is to use the Add Library Button. Other libraries can be added with the Project -> Add Include Path and Add Library Path.)
  • TymkrsTymkrs Posts: 539
    edited 2013-08-29 09:32
    dgately wrote: »
    Sorry, should I not have brought up the issue? :innocent:

    Anyway, I just thought of the "newbie" C users and how they might react to compiling example code. It could be very frustrating if the example code they are learning from does not compile without edit.


    dgately

    Hey dgately, no not a problem :p I learned something from your post about an easily visible item on SimpleIDE so that's a good thing! And you are right, it would be good for code to compile :p. I'll be more careful in future situations.
  • jazzedjazzed Posts: 11,803
    edited 2013-08-29 10:41
    Tymkrs wrote: »
    Hey dgately, no not a problem :p I learned something from your post about an easily visible item on SimpleIDE so that's a good thing! And you are right, it would be good for code to compile :p. I'll be more careful in future situations.
    Any feedback on the easily visible Help menu and contents (such as the User Guide) would be appreciated.
  • TymkrsTymkrs Posts: 539
    edited 2013-09-10 14:38
    Welcome to the eighth episode of FirstC! This is a short podcast series to help beginner coders learn how to program in C on the Parallax Propeller! This week, we talk SimpleIDE and 7 segment displays! We address some issues that came up in the Parallax forums about standards and go through other selectable properties in SimpleIDE's menus. We also go through a step by step method of figuring out how to wire up seven segment LED displays and make them show alphanumeric values!

    Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-008/


    Each segment individually: http://pastebin.com/7ZwrPKG0
    a-y long code version: http://pastebin.com/k38skz7Y
    0-9 a-y abridged: http://pastebin.com/Ng4nNGBM
    Error: http://pastebin.com/ai9PAh8e
    Picture of Error: http://i44.tinypic.com/2h58od2.jpg
  • jazzedjazzed Posts: 11,803
    edited 2013-09-12 18:42
    Hi.

    Congrats on Episode 8.

    One clarification. If you don't specify -std=c99, you will get the gnu standard as of GCC 4.6.1 which includes much of C99. To get C89, you must say -std=c89 or -std=ansi.

    Another item may be unclear because of the way Qt works on Windows. The project side-bar in project view can be hidden if you grab the line between the project side-bar and the editor by click and drag all the way to the left. It is also possible to hide build status, but that is left as an exercise. On Mac and Linux the grab bars are more obvious.

    Also, if you really want access to file button items in simple view, you can right click on the toolbar and check File to show the file buttons. Hiding the file menu in simple view was required by Parallax as a way to focus on just one set of tools so that users don't get confused about what to do - use at your own risk :).

    --Steve
  • dgatelydgately Posts: 1,631
    edited 2013-09-12 19:33
    Tymkrs wrote: »
    Welcome to the eighth episode of FirstC!

    In answering the big question from the eighth podcast "Does saving the project in Simple View, save the currently edited file?", the answer is Yes! It saves the entire project (yes, you can have many files within a project) and all of its files and settings!

    And even though you are in Simple View when editing a file, Control-S (or Command-S on Mac OS X) will save the currently edited file... Even though the File menu is not showing at that time, the command is still available from the keyboard (yay!).

    Thanks for all your updates in the podcast about the last episode's comments...

    Take care,
    dgately
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2013-09-12 22:57
    dgately,
    I was pretty sure that what you said was true, but Atdiy kept saying that her files were not saving somehow. I think it may just have been confusion on her part.

    jazzed,
    Thanks for the clarification on the way GCC works with and without the -std-c99 flag . I mostly work with MSVC++ and g++ (gcc c++ mode), so I'm not as readily familiar with the straight C stuff in gcc.

    I personally prefer the Project mode (obviously), and can't stand the Simple view mode :P. I understand why it's there, but I don't have to like it or use it! Although, for the show I do switch back to that mode to check things.


    Everyone,
    We really like getting the feedback and comments here. We think it makes for a better show to have some of it be talking about and/or answering your comments/questions. There's a lot of things I wouldn't think about talking about because it's so "obvious/trivial" to me, but to new folks it's not. Atdiy hits a lot of those things with her questions, because she's been doing this for a while now with First Spin and now 8 shows of First C, she's becoming less of a "new to it all" person. So please keep those comments and questions coming! :)

    Roy
  • Mark MaraMark Mara Posts: 64
    edited 2013-09-13 06:24
    Hi,

    I've been enjoying your shows. Here are some random thoughts on future content.

    My experience with C is that as I build an application at some point it starts doing some very wierd stuff. Errors move and warnings change when you insert debug statements. This usually means that I am stepping on memory somewhere. A discussion on how to debug this situation would be great. This would include how the stack is used and the implications of recursion.

    While I can write code C, but I am not a C programmer. I am interested in learning the C way of doing things, particulary in the context of using it to develop propeller apps. Examples of "gold standard" C code would be great, but probably beyond the scope of your shows. I have been using the propgcc libraries as my examples of "good" C/propeller code.

    How about spending some time on compiler and linker options ( other than the check boxes)? Also, some time spent on how to use a map file might also be useful.

    Thanks for doing the shows.

    -markM
Sign In or Register to comment.