Author Topic: arduino question  (Read 4640 times)

0 Members and 1 Guest are viewing this topic.

johnnym

  • Jr. Member
  • **
  • Posts: 52
  • Country: us
arduino question
« on: February 14, 2019, 04:25:02 AM »
I saw the getting started with uno post at the top but have some questions. I have a couple of Arduino kits (uno and mega)with all kinds of components. It seems that if you are not competent in C or C++ then all you can do is copy the typical projects and not really create anything on your own. The only thing I can do with absolutely no help (not looking up code or schematics) is with a bunch of LED's. You know, make them blink in different ways, whatever I want them to do. Printing what they do on the serial port (I find pointless but probably worth knowing) etc... It is more of the software side than the hardware that is my problem. If you don't know how to code and use the proper syntax then you get stuck. My goal was to build stuff without any external help but that seems impossible.

They (I guess the general consensus) say it is a C/C++ language but it seems to lean more towards C++. I have always been more interested in the hardware but since my programming skills are at a beginner level I am stuck.

Would learning C++ or C help or translate over to the Arduino IDE? You know, in order to make the coding easier? C++ is supposed to be more involved than C? I just don't get programming. How can I learn C or C++ without spending a ton of money? A guide would be nice. At the rate I am going it will take years to create anything actually original on my own without no one showing me the way. Where can I learn to code properly? Any suggestions ?     //frustrated hobbyist ranting about programming



DamonHD

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 4125
  • Country: gb
    • Earth Notes
Re: arduino question
« Reply #1 on: February 14, 2019, 08:39:59 AM »
I don't *think* that I paid for any C++ learning stuff ever, and have done it consultant/professional level in finance and for embedded.  There is lots of free material out there, and you can learn the bits that you need to, at your own pace.

Our Radbot product has many 10s of thousands of lines of C++, all compiled through the Arduino IDE (running on the same AVR chip as in the UNO).  It's not the most sophisticated build environment that I've ever used, but it is good and robust.  We have many thousands of devices in the warehouse with Arduino-compiled code!

I suggest that unless you want to do complicated things, handling significant amounts or complexity of data, you can largely ignore the C++ and work more at the C level if that is easier to get to grips with.

Rgds

Damon
Podcast: https://www.earth.org.uk/SECTION_podcast.html

@DamonHD@mastodon.social

Bruce S

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 5370
  • Country: us
  • USA
Re: arduino question
« Reply #2 on: February 14, 2019, 09:15:24 AM »
I agree with Damon.
I can barely code with basic, yet it seems that's enough to get started doing the more complicated stuff.

Operhouse has become my hero when it comes to the Arduino stuff  8), I believe he's self taught.

What I've learned is not to get so deep into coding that you lose yourself in it.

My current project is running my hydroponic pump using an UNO. nothing fancy, turn it on for (X) amount of time then off for (X) amount of time. Once it's stable, I'll move onto turning to grow lights on , etc.

Arduino has online classes
 https://www.arduino.cc/en/Tutorial/HomePage?from=Main.Tutorials

Hope this helps.

Bruce S
A kind word often goes unsaid BUT never goes unheard

OperaHouse

  • Hero Member
  • *****
  • Posts: 1308
  • Country: us
Re: arduino question
« Reply #3 on: February 14, 2019, 02:28:08 PM »
There is plenty you can do without knowing C.  I have shown all my code using only IF statements in sequential order so beginners can easily follow.  Pretty much conversational English.   Good code is almost unreadable. If it was there would be no reason to hire the guy.  You will get really screwed up with things like if (y = 12) which seems reasonable when you see it. This being a logic operation, (y == 12) only works. It is easy to never see that error when things don't work.  It is picky. Create some boilerplate programs that have the basic stuff already ordered and add code to that. The sample programs are good for that.  I find it a real headache starting from nothing. Boilerplate lets you get into the code right away. Write up one function at a time and get that to work before going to the next section of code. Success builds confidence. Avoid do and for code.  It may be compact but it is easy to get lost. Avoid shortcut coding. When you come back in 6 months it will be much harder to figure out when you have forgotten the language tricks like X++1. No one will see your code. No need to be embarrassed by clunky code that works.  Always use  a serial line at the end that reads out the variables. Helps a lot when trying to diagnose a problem.

Bruce S

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 5370
  • Country: us
  • USA
Re: arduino question
« Reply #4 on: February 14, 2019, 02:54:40 PM »
OperHouse
 Thanks for chiming in.

johnnym
Also; you could post what code you're working with or the plan of what you're attempting here.

We could always help too.

Cheers
Bruce S
A kind word often goes unsaid BUT never goes unheard

JW

  • Development Manager
  • Super Hero Member Plus
  • *****
  • Posts: 4019
  • Country: us
    • Flashsteam.com
Re: arduino question
« Reply #5 on: February 14, 2019, 04:42:22 PM »
Well since "boilerplate" was mentioned.

I have a link to Arduino and its a topic I created. Its on Steamautomoble.com

https://steamautomobile.com:8443/ForuM/read.php?1,26164

Why arent you guys talking about "sketches"

Bruce S

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 5370
  • Country: us
  • USA
Re: arduino question
« Reply #6 on: February 14, 2019, 04:48:39 PM »
Why arent you guys talking about "sketches"

Too early in the AM  ;D
A kind word often goes unsaid BUT never goes unheard

SparWeb

  • Global Moderator
  • Super Hero Member Plus
  • *****
  • Posts: 5452
  • Country: ca
    • Wind Turbine Project Field Notes
Re: arduino question
« Reply #7 on: February 15, 2019, 01:07:24 AM »
Something that helps me is to write "pseudocode", which isn't programming at all.

More like a series of statements written in plain english that you can read through to logically see the flow of the program.

On the first pass, just write the obvious general steps...
  • Set up the input variables,
  • Ask user for input,
  • Calculate result,
  • Display result on screen,
  • Send result to serial port.

Once that's done, go to each line and add detail...
  • Set up the input variables,
    • length, width, height, mass
    • Volume, density
  • Ask user for input,
  • ...

This lets you read and correct your logic as you flesh out what you want the program to do.
No one believes the theory except the one who developed it. Everyone believes the experiment except the one who ran it.
System spec: 135w BP multicrystalline panels, Xantrex C40, DIY 10ft (3m) diameter wind turbine, Tri-Star TS60, 800AH x 24V AGM Battery, Xantrex SW4024
www.sparweb.ca

DamonHD

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 4125
  • Country: gb
    • Earth Notes
Re: arduino question
« Reply #8 on: February 15, 2019, 02:15:40 AM »
And then those should then stay in the code as comments, to explain what you intended to do, so that when you have a bug (and did something different by mistake) you can get back to what you intended...

Rgds

Damon

Podcast: https://www.earth.org.uk/SECTION_podcast.html

@DamonHD@mastodon.social

clockmanFRA

  • Hero Member
  • *****
  • Posts: 952
  • Country: fr
    • Renewable Energy creation
Re: arduino question
« Reply #9 on: February 15, 2019, 02:35:31 AM »
Sparweb    Excellent, .... I love fundamental structures.

I like that, now your talking!

Please do carry on.......
Everything is possible, just give me time.

OzInverter man. Normandy France.
http://www.bryanhorology.com/renewable-energy-creation.php

3 Hugh P's 3.7m Wind T's (12 years) .. 5kW PV on 3 Trackers, (8 yrs) .. 9kW PV AC coupled to OzInverter MINI Grid, back charging AC Coupling to 48v 1300ah battery

frackers

  • Sr. Member
  • ****
  • Posts: 435
  • Country: nz
  • Picard spits "Hello"
Re: arduino question
« Reply #10 on: February 15, 2019, 06:03:51 AM »
Although I wrote my first software 52 years ago, I've only been writing embedded, realtime software for about 40 years. I started with AVR about 20 years ago just as Atmel released it (I still have a sample AT90S1200) and so had to write everything in assembler as there were no compilers available!!

From this perspective I couldn't agree more with the approach being described by sparweb.
  • Define the problem
  • create the outline (the recipe you want the computer to follow)
  • fill in the details in english language
  • keeping the outline and details as the comments, start filling in the code. This is where a good compiler is essential as it will flag most of the obvious errors in syntax (Arduino uses gcc under the hood)
  • comments are reminders of WHY you are doing something, the code says how. I certainly can't remember what a piece of code is supposed to do after 5 years so write it down!

Probably not useful for this discussion but some of my stuff is on github at https://github.com/g8ecj. The tunnel house controller project is the least complex but even there it is using a large standard library (BeRTOS) which makes some functions less than obvious!! The 'User Interface (UI) code on the other hand for the wind turbine controller is a rats nest of structures and lookup tables to make it as flexible as possible.
Robin Down Under (Or Are You Up Over)

johnnym

  • Jr. Member
  • **
  • Posts: 52
  • Country: us
Re: arduino question
« Reply #11 on: February 15, 2019, 11:41:57 AM »
Most of what I have learned is from that dude from toptechboy.com and random tutorials online. I guess the overload of info online makes it hard to choose something as a guide because some of the tutorials I found online were terrible. The terrible tutorials were designed in a way where you didn't really learn anything (at least with myself) or couldn't add on to what you already know. "Don't worry if you don't understand everything right now" is what I keep reading/hearing in these tutorials.

These are some of the examples of what I know and they just feel memorized and not fully understood. (Like I understand 25% of what is going on).

LED and Button

int ledPin = 3;
int buttonPin = 2;

void setup() {
 
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT_PULLUP);
}

void loop() {

  int buttonState = digitalRead(buttonPin);
  digitalWrite(ledPin,buttonState);

}





serial port printing w/led's

int redLedPin = 9;
int redOnTime = 250;
int redOffTime = 250;
int yellowLedPin = 10;
int yellowOnTime = 250;
int yellowOffTime = 250;
int numRedBlink = 5;
int numYellowBlink = 5;

void setup() {
  Serial.begin(9600);
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
}

void loop() {

  Serial.println("the Red LED is blinking");
    for (int j = 1; j <= numRedBlink; j = j + 1) {
     Serial.print(" You are blink #: ");
     Serial.println(j);
     digitalWrite(redLedPin, HIGH);
     delay(redOnTime);
     digitalWrite(redLedPin, LOW);
     delay(redOffTime);
  }
Serial.println(" ");

Serial.println("the Yellow LED is blinking");
for (int j = 1; j <= numYellowBlink; j = j + 1) {
  Serial.print(" You are blink #: ");
  Serial.println(j);
  digitalWrite(yellowLedPin, HIGH);
  delay(yellowOnTime);
  digitalWrite(yellowLedPin, LOW);
  delay(yellowOffTime);

}
Serial.println(" ");
}


These are just two examples. I have physically written down every program (around 15) I have done so that I can recreate them by memory or lack of memory. Basically, building upon the basic blink example and expanding on it is what I have done.   Maybe this approach is just stupid because I don't really understand what is actually going on, lol. Confusion with where you can and can't use integers, variables, statements, what can and can't be an OUTPUT/INPUT, global variables, for loops, etc... it is all so confusing. The "don't be worried if you don't understand everything right away" phrase is what I keep emphasizing to myself. Thinking that through all of the confusion it will eventually "click". Nothing has clicked yet, lol. A buddy of mine who works in IT says I am technologically retarded, lol. I think he is right. Always 10 years behind the times it seems.


Adding // or /* comments is something I did for a while but stopped because this is a hobby for me not a profession. Not meaning that I don't take it seriously. Thanks frackers, the importance of comments is something that I have ignored.  Building a digital clock, dusk to dawn solar powered light, intrusion alarm, flashlight, weather station, rfid or I guess it would be a gps tag for my dog, etc.. are all things I would love to do before I die because it seems fun (although frustrating) to do.





johnnym

  • Jr. Member
  • **
  • Posts: 52
  • Country: us
Re: arduino question
« Reply #12 on: February 15, 2019, 11:58:39 AM »
thanks everybody, after reading through all the comments here the //comments make more sense.

SparWeb

  • Global Moderator
  • Super Hero Member Plus
  • *****
  • Posts: 5452
  • Country: ca
    • Wind Turbine Project Field Notes
Re: arduino question
« Reply #13 on: February 16, 2019, 01:06:26 AM »
"overload of info online"

I hear ya.

When I started on this datalogger project:  https://www.fieldlines.com/index.php/topic,149346.0.html
...I knew I had to be very methodical about the programming.  The program to run the thing was going to get very long.  If I didn't work it out as "pseudocode" first to get the logic to make sense I probably would have failed, or got lost so many times that debugging would have taken twice as long.

As it was, the datalogger took several months to finish.  Later, I broke my own rule after I added a number of components to the board (LCD and a temperature sensor) and neglected to pseudocode the program to run the new features.  Troubleshooting that stuff took longer than the basic parts.  It went from 200 lines of code to almost 1000 lines of code before a big clean-up that brought it down to 500 lines.  Two dozen subroutines (or "functions) so it was a bear to wrestle.

I'm afraid the process of writing the pseudocode means I didn't keep it to show you a complete example.  One of my earliest passes looked like this:

Code: [Select]
  /* PSEUDOCODE
   *  each interrupt routine that is called performs a specific task required during its part of the cycle
   *  Phase 1: record current time in "micros()"
   *  Phase 2: must pass through this phase before proceeding.  Don't care how long this takes   
   *  Phase 3: next pass through this phase before proceeding   Don't care how long this takes
   *  Phase 1: return to start.  speed = 1/Elapsed time.
   *            Elapsed time = (now) - (last phase 1 time)
   *  Each time entering the specific phase's interrupt, make sure to toggle the previous phases's state record.
   *  If any random phase is triggered, only when it is the next successive phase will the correct action
   *  occur, and a conditional statement can be written to detect the correct conditions.
   *  Use boolean operators to keep track of the states of each phase, ie. "boolean phase1, phase2, phase3;"
   */

The pseudocode grew from there to a whole page long before I started writing commands into it.  And as DamonHD rightly pointed out, it remained in the comments lines for a long time so that I could remind myself what I was trying to do in each part of the program.  It also got added to and I would say my datalogger program file size is 1/2 comments.
No one believes the theory except the one who developed it. Everyone believes the experiment except the one who ran it.
System spec: 135w BP multicrystalline panels, Xantrex C40, DIY 10ft (3m) diameter wind turbine, Tri-Star TS60, 800AH x 24V AGM Battery, Xantrex SW4024
www.sparweb.ca

richhagen

  • Hero Member
  • *****
  • Posts: 1597
  • Country: us
Re: arduino question
« Reply #14 on: February 16, 2019, 05:45:54 AM »
I have learned programming in the past, but not C++ or any C language.  I picked up the Arduino coding pretty easily.  I would recommend that you watch a series of videos on youtube by Jeremy Blum as they are good tutorials on the basics of programming the Arduino.  Then just start building stuff.  I have built quite a few control things using Arduinos sensors and relays, both solid state and magnetic relays.  It is a relatively easy way to put a bit of logic and automation into into controlling things.  Rich
A Joule saved is a Joule made!

Mary B

  • Administrator
  • SuperHero Member
  • *****
  • Posts: 3169
Re: arduino question
« Reply #15 on: February 16, 2019, 04:06:52 PM »
When I learned programming commenting on what each section was doing and what the variables were was repeated over and over again. That way if you come back to it a year down the road you know WHY you wrote it that way... even if it now seems a stupid way to do it because you have learned more.