Notifications
Clear all

Old Programer trying to learn new tricks

9 Posts
6 Users
1 Reactions
207 Views
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

Hello All,

I’m glad to join the forum cause I’m gonna need a butt load of help. 

I’m an old geezer that was programming back in the 1970s. Spent many years programming in assembly language, Fortran, basic, cobol.  All the classics. In the 80s spent many a night bread boarding and programming my own single board controller. 

now retired and bored, I wanted to play with arduino controllers so I picked up one of the kits of Amazon and have started playing around. 

My programming was done with calls to subroutines. I got out just as the object oriented philosophy was coming on line. So forgive me if in posts I don’t use the correct terminology for methods or what ever else they are called now a days. 

Lom



   
Quote
tfpfau
(@tfpfau)
Member
Joined: 4 months ago
Posts: 12
 

Welcome. Starting in the late 70s, I have programmed in several assemblers (6800, 8080/Z80, 8086, PDP-8, PDP-11, VAX), BASIC, FORTRAN, COBOL, Pascal, C/C++, and several scripting languages. I'm sure there are others here who have similar experience. You have plenty of friends here.

There's not too much magic in object oriented languages. When you implement a library for some purpose you usually collect context in a structure that gets created by some initialization function. Then you would pass this structure to the functions in your library and they would update the contents. In OO the initialization function is called a constructor and the structure is passed to the functions (methods) as a hidden argument. It's just a bit more formalized.



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@lom

My programming was done with calls to subroutines. I got out just as the object oriented philosophy was coming on line. So forgive me if in posts I don’t use the correct terminology for methods or what ever else they are called now a days.

 

Also an old geezer but have only programmed at a self taught hobby level from the early assembler days to today and combined that with my interest in electronics.

When programming an Arduino board you might want use library code that contains classes and make use of their methods rather than write your own functions.

Here’s a simple chatGPT example of using a class from the Arduino Servo Library to control a servo motor.

 

### Basic Example: Sweep a Servo

```cpp
#include <Servo.h>  // Include the Servo library

Servo myServo;      // Create a Servo object

void setup() {
  myServo.attach(9);  // Attach the servo to pin 9
}

void loop() {
  // Move from 0 to 180 degrees
  for (int pos = 0; pos <= 180; pos++) {
    myServo.write(pos);  // Set servo position
    delay(15);           // Wait for it to move
  }

  // Move from 180 back to 0 degrees
  for (int pos = 180; pos >= 0; pos--) {
    myServo.write(pos);
    delay(15);
  }
}

 

### How the Class Is Used

* `Servo myServo;` → creates an instance (object) of the Servo class
* `myServo.attach(pin);` → tells the object which pin controls the servo
* `myServo.write(angle);` → sends a position command (0–180°)

### Key Methods from the Servo Class

* `attach(pin)` – connects the servo to a pin
* `write(angle)` – sets angle (0–180)
* `writeMicroseconds(us)` – more precise control
* `detach()` – stops controlling the servo

 

 

 



   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

Robobuilder,

thank you for the explanation and example.  I will have to start thinking in terms of objects and it looks like the methods pass the parameters and take actions and probably return values (although not in the example).

two questions

1. can instances of objects be declared in the setup loop or must they be done before that, as in the example?

2. Where can the object declaration and methods documentation be found?

thanks



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@lom 

There is no "setup loop" just setup() which is executed once and loop() which keeps repeating.

Don't be afraid to use chatGPT to give you answers. It will respond quickly and keep clarifying anything you don't understand in an ongoing exchange.

Type (or copy/paste) this into the chatGPT text box

Using Arduino C++ can instances of objects be declared in the setup loop or must they be done before that, as in the example?  Where can the object declaration and methods documentation be found?

 



   
ReplyQuote
tfpfau
(@tfpfau)
Member
Joined: 4 months ago
Posts: 12
 

@lom You can declare global variables in the space outside of functions. Variables declared inside of a function are only visible until the end of the function. setup() is a function so any variables declared within it disappear when it returns.



   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2042
 

Hi @lom,

  Arduino IDE (and other IDEs), are based on C++. In case you hadn't realised, the 'classic' language that spanned direct access to hardware and higher level software design is C, dating back to around 1970, and introduced to many by the Kernighan and Ritchie C Programming manual ... a thankfully thin, but fairly comprehensive starting point.

About 1985, C was 'expanded' by Bjarne Stroustrup, who also introduced his language C++ with a book, rather thicker than the C book, The C++ Programming Language.

It appears, C++ started life being called C with Classes, which gives a strong hint as to its general philosophy. Namely, C++ tried to be an upgrade to C, such that most C programmes could readily be transferred to a C++ compiler, with most of the C programme compiling without change, and the main changes were associated with C code that allowed the programmer to make mistakes like passing an integer to a function expecting a float, but not flagged by the compiler. Thus, with just a little care and flexibility, it is possible to 'start' by 'mainly' programming in C, and gradually building in the C++ extensions.

In practice, many applications use pre-built libraries, which utilise classes as their basic design, and hence you will sometimes see objects defined and used in a manner that is only a small extension from the C syntax for built-in types, such as 'int', for example the DHT22 temperature sensor; 

    int   mynumber; 

compared to:

    DHT_Unified    dht(2, DHT22);

Then, the object dht can be utilised, by calling its methods (method is the C++ name for a function owned by a class), such as:

  dht.begin();

where begin is the method built in to the class to initialise it.

---------

Some common classes are defined in the background of the Arduino IDE.

e.g. Arduino boards have at least one serial port that communicates with the host computer's terminal window, so that the programmer can see messages from the board.

e.g. 

Serial.begin(9600);

Serial.println ("DHT22 Sensor Example");

which starts the serial link at speed of 9600, and prints a sign on message, using the println method 

The code to define the object "Serial" does not need to (and should not) be defined by the programmer.

-----------

Thus, while it is handy to be totally fluent in the latest version of C++, which has extended considerably from the Bjarne's original definition, it is possible to build useful products using the supplied libraries and knowing C, together with a few 'snippets' of C++. 

However, if you start looking inside the many library source examples, you will need to extend your C++ understanding.

So I suggest you start by looking at C syntax, and then have a quick look at the Class/object concepts and syntax of C++, so that the context of the extra  'C++ snippets' needed can be understood, without getting lost in the detail.

I hope this waffle makes a little sense. Obviously, becoming fully fluent in C++ would be 'ideal', but it is now a complex language, and the learning curve is likely to be hard, so I have tried to suggest a way of starting.

Good luck and best wishes, Dave



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
 

@davee, @lom,

I don't want to hijack this intro, so I'll be creating a separate thread in the C++ forum for a reply. I have to give it some thought so it will take a bit of time to compose my thoughts.


The one who has the most fun, wins!


   
ReplyQuote
(@saneme)
Member
Joined: 2 months ago
Posts: 1
 

OLD OR YOUNG, YOUR BRAIN IS WORKING- THAT IS THE POINT. I MYSELF BELONG TO "BABY BOOM" GENERATION. BEST WISHES



   
ReplyQuote