Notifications
Clear all

Where one can self-educate on understanding arduino.

24 Posts
9 Users
7 Likes
1,864 Views
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

This is a bit beyond the original poster's request for resources, but I am confused by all the references to C++. I can read and write C code, but I cannot discern C++. I have seen object code for sensors and actuators, and I recognize it as C++. I am assuming that function calls like analogRead() or digitalWrite() are object code methods, and not native to C. An instance of a serial object like Serial.begin(), is call to the begin() method of a serial object in the I/O library. Please clarify and correct me if I am wrong. 

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
jker
 jker
(@jker)
Member
Joined: 3 years ago
Posts: 82
 
Posted by: @zoolandermicro

I am assuming that function calls like analogRead() or digitalWrite() are object code methods, and not native to C. An instance of a serial object like Serial.begin(), is call to the begin() method of a serial object in the I/O library. Please clarify and correct me if I am wrong. 

analogRead() and digitalWrite() are pretty much basic C function calls. They come to you as a user in the form of compiled object code, but from a programmer's perspective you treat them exactly the same way. They were actually written as C functions, as you can see at the ArduinoCore github ( https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring_digital.c#L138), which was then compiled to object code before including with the downloadable package.

 

In C, you have the ability to create "structures", more or less a bundle of data that can be treated as a single variable. In Computer Science-speak these are "composite types" (ie, types composed of other types)

typedef struct MyStatusStruct {
int temperature;
int pressure;
char digit;
};

At this point you can create objects of this type with lines such as:

void loop() {
MyStatusStruct myStatus;
myStatus.temperature = analogRead(PIN_TEMP);
myStatus.pressure = analogRead(PIN_PRESSURE);
myStatus.digit = ReadDigitsFromKeypad();

MyFunctionThatChecksForSomethingImportant(myStatus);
}

In this case, the structure isn't doing anything terribly useful, but it serves to show the syntax.

One major improvement in C++ is the ability to tie functions to these structured types. Now can write...

struct MyStatusStruct {                     // typedef keyword is unneeded here in C++
int temperature;
int pressure;
char digit;
void CheckForSomethingVeryImportant(); // This is now a "member function" of this type

};

void loop() {
MyStatusStruct myStatus;
myStatus.temperature = analogRead(PIN_TEMP);
myStatus.pressure = analogRead(PIN_PRESSURE);
myStatus.digit = ReadDigitsFromKeypad();

myStatus.CheckForSomethingVeryImportant();
}

At a conceptual level, the function is "connected" to the data of the object. And, indeed, the CheckForSomethingVeryImportant function can use all of the members of the struct as if they were local or global variables.

 

This is actually what is going on in with the Arduino serial library. There is a struct (or class... the keywords are interchangeable in C++ with one caveat) called SoftwareSerial... and by default a single global variable of this type is created called Serial.

When we call Serial.begin(115200), that's calling the function begin(long speed) "on" that global object named Serial, configuring it for use. Later "member function calls" such as print() have access to shared state, since they're all using that same object.

To take full advantage of this, there's nothing actually stopping you from creating a second SoftwareSerial object (using different pins on the arduino) and having different active lines of communication. The creators of the Serial system actually produced a small example ( https://www.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample)

"A resistor makes a lightbulb and a capacitor makes an explosion when connected wrong"
"There are two types of electrical engineers, those intentionally making antennas and those accidentally doing so."


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @zoolandermicro

This is a bit beyond the original poster's request for resources, but I am confused by all the references to C++. I can read and write C code, but I cannot discern C++. I have seen object code for sensors and actuators, and I recognize it as C++. I am assuming that function calls like analogRead() or digitalWrite() are object code methods, and not native to C. An instance of a serial object like Serial.begin(), is call to the begin() method of a serial object in the I/O library. Please clarify and correct me if I am wrong. 

The ISO C++ Standard Library encompasses (not as much these days), most of the ISO C Standard Library, so much so that it is often referred to as a superset of C, though not 100% true – As such, it is highly compatible with C, and you can write and compile most C code within a C++ compiler, but not the other way around.

C++ is a multi-paradigm programming language, where everything is an object. For example, a structure in C cannot have member functions (or methods if you like), so you can’t create an object and then deference it’s member function using a dot operator such as in Serial.begin(). Whereas, in C++, a structure is actually considered a class with public access as the default, and can encompass member functions that stay with the object instance whenever passed around by reference or value.

The biggest problem is that because many people keep perpetuating the view that Arduino is using C, newbies therefore think that they need to (or should) learn C first, which has pointers and all that scary stuff, whereas C++ does it’s best to avoid using those types of constructs (even though it can), in favor of using C++'s built in objects, and even creating your own.

There are many advantages to learning C++ over standard C, over just writing C in a C++ environment. Every Arduino newbie is using the built in C++ objects already, but won’t know any better if they are not properly informed.

Anyway, this is just the tip of the iceberg, and to prove that you are actually working under a C++ environment (that accepts most legacy C program code), go to your preferences under your IDE menu and turn up the compiler logging levels, and compile any sketch. In the bottom of the IDE debug window, you can then search the compiler messages and you should find something like the following line, depending on the O/S platform you’re using – This is an example under the Windows O/S:

"C:\\Users\\<USER>\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++"

As you can see, the Arduino environment we work with compiles our sketches using: "avr-g++".

Please let me know if you need further clarification.

Cheers.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@jker

Good explanation, but again... it's just the tip of the iceberg!

I'd love to see more people learning and using C++ to their advantage (at least the features made available under a cut down embedded version, as they are still quite powerful).

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

RobotBuilder wrote: If you were to attach a POT to the hinge to measure the degree of openness the door would suddenly have many recognizable values and thus many possible states.

frogandtoad wrote: In theory perhaps, but I don’t recall his requirements calling for that many states, nor does his state chart diagram represent that.

I digressed off topic into theory in response to "how many states does a real door have" not how many states the program may have. From the point of view of the program there is no "door" only an input we label DOOR and give two values OPEN or CLOSED.

I would have gone about it differently as I am not familiar with the high level abstractions that were applied to the problem. From a safety point of view I would throw out the current door and replace it with a self closing, self locking door rather than hope someone will get to it and close it manually before the baby took a tumble while following big brother. I would also have a sound alarm everyone could hear as I am not familiar with how to trigger an Alexa response which I assume would also be loud enough for anyone to hear?

 


   
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

@frogandtoad @jker  Thanks so much for your response. I have the book C++ Programming 101 (Greg M Perry). This is another book that I started and put down. Not because it was dry or too hard to follow. I got this book because I liked this author's book C Programming Absolute Beginners Guide. I just got side tracked. I will definitely pick it up again. Thanks for the encouragement and inspiration.  

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
Sid
 Sid
(@sid)
Member
Joined: 3 years ago
Posts: 111
 
Posted by: @robotbuilder

All that is required is an understanding of basic digital and analog electronic circuits of which the microprocessor is but one module.

I am trying to find some of these places where one can learn the basics of Digital and Analog electronic circuits. Reason being, me - I practically have not even the basic skills - I know how to code, have learned most of it (Arduino coding) from online sources. But practically, I have not dared to extend beyond those LEDs and the stuff they show on their tutorials.

Life is exploring and learning


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

The Arduino development system is not the most simple way to start learning basic electronics. It is essentially a little computer that can turn switches on and off (output) or read switch on/off states (input).

When it comes to using your Arduino to control electronic stuff you are essentially writing a program to turn switches on or off or to read if a switch is on or off.  Learning how the electronics actually works is another a ball game.

You have to start with the basic electrical concepts and how to measure them.  A simple circuit is like a simple function.  Complex circuits are made out of simpler easy to understand circuits just as larger functions are made out of simpler easy to understand functions. The whole circuit is like the main function in a computer program.

Just a quick search of the internet turned up lots of resources for learning basic electronics just find something that bests suits your needs.

https://www.makerspaces.com/basic-electronics/
http://engineering.nyu.edu/gk12/amps-cbri/pdf/Basic%20Electronics.pdf
https://cdn.instructables.com/ORIG/F04/4ZOP/JI4UABFW/F044ZOPJI4UABFW.pdf
https://www.n5dux.com/ham/files/pdf/Electronics%20for%20Dummies.pdf
https://discourse-production.oss-cn-shanghai.aliyuncs.com/original/3X/c/a/ca09c8454fd0e3133f53c21363edf4f4993b4f18.pdf

 

 

 

 

 

 


   
Sid reacted
ReplyQuote
Sid
 Sid
(@sid)
Member
Joined: 3 years ago
Posts: 111
 

@robotbuilder

Agreed, for sure, I agree, Arduino is not the place to start to learn the Basics of electronics.

Decades ago, I was exposed to Soldering and LEDs. I then made an audio cassette player. Then it was all shelved because life had different paths for me and I took up computer programming. Then during this year, the C-19 happened. Lost my job and stayed home all day. So I started surfing YouTube and somewhere I found Charlieplexed LED cube. The device in use was Arduino Nano and that got me into Arduino.

It was only when I watched videos from Paul McWhorter and DroneBot, I realized that I need to learn the basics if I need to move ahead.

Thank you for these links. Have bookmarked them all and will be following up the ones.

Life is exploring and learning


   
ReplyQuote
Page 2 / 2