Notifications
Clear all

Problem uploading sketch.

125 Posts
6 Users
1 Likes
41.2 K Views
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@dorsay

Hi Dave,

Posted by: @dorsay

there are a number of black holes

don't hesitate to ask...

Posted by: @dorsay

I'm waiting for more servos to arrive

if you reduce the number of managed servos (#define NB_OF_SERVOS) from 5 to a lower number (and reduce the number of initialization values in the arrays to the same number), you should be able to test it even starting with only 1 servo

Posted by: @dorsay

I have a bunch of questions regarding your sketch (if you don't mind)

go ahead ! ? 

Posted by: @dorsay

But one of the things that I find is missing is a method of stepping through a sketch so that one can follow the path of the program.  What do you use?

If you mean the line-by-line execution of a sketch that you follow in a debugger, showing you variable values etc..., then i don't use anything at the moment : it requires both a debugger capabality of the IDE, and a physical interface between the microcontroller and the debugger.

I plan to use the PIO Unified Debugger of PlatformIO on Visual Studio Code, with an ESP32 and a JTAG debugger interface.

But this required an interface from the microcontroller, and the Arduino Nano/Uno/Mega doesn't have that...

Anyway I consider VSCode/PlatformIO far superior to Arduino IDE, and I use it to play with all my Arduinos, STM32's, ESP8266's and ESP32's

Eric


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

Posted by: @zeferby

Anyway I consider VSCode/PlatformIO far superior to Arduino IDE, and I use it to play with all my Arduinos, STM32's, ESP8266's and ESP32's

Do you mean you write your sketches using VSCode/PlatformIO?  VS=Visual studio?

David


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@dorsay

I use Arduino IDE some times and VSCode/PlatformIO other times, depending on my mood ? ...VSC/PIO more and more often as I am quite fond of it's very large ecosystem of contributed extensions.

Also being a coder by trade, I prefer more conventional IDEs with the usual lot of developer features like Intellisense, direct access to a symbol declaration or definition, etc...all of which I miss with the Arduino IDE

Eric


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

A couple of easy questions;

1  What's the reason for using uint8_t rather than just int?  Is memory that critical?

2  Just before void loop, you have 

"}//void setup()"

then later you have 

"}//void loop()".  What are these for?

I presume they're something to do with "#define TEST_TUNE"?

3  The syntax of "#define" is "define constantName value" but you have "define TEST_TUNE" -  no value?

4 "#define is a useful C component that allows you to give a name to a constant value before the program is compiled."  That's according to the Arduino Reference PDF.  But why not just use "constant"?

David


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

Eric,  it's after 10pm here so I'll stop bothering you tonight.  Thanks for all your advice so far.  

David


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@dorsay

Posted by: @dorsay

1  What's the reason for using uint8_t rather than just int?  Is memory that critical?

Memory is not a problem with that sketch as it is; it's just that I started by checking the source code of the Adafruit library, which uses proper portable types (uint8_t, int16_t, etc...) and when i wrote the sketch, using Arduino IDE "standard" types, I did not change that one.

You can as well use an int, and anyway the only place I call the function, I give it an int parameter (iServo) (which is bad practice because a signed 16-bit integer type-casted to an unsigned 8-bit one is bad ?...which is what we'll do anyway when we call pwm.setPWM(iServoNumber, ... because setPWM is declared as void setPWM(uint8_t num, uint16_t on, uint16_t off); in the Adafruit library, so we are lazy programmers ! ? Thankfully you should not have negative or large values for the servo number).

So you can just declare checkMoveServo as void checkMoveServo(int iServoNumber, int iNewSwitchState) if you want.

Posted by: @dorsay

2  Just before void loop, you have 

"}//void setup()"

then later you have 

"}//void loop()".  What are these for?

I presume they're something to do with "#define TEST_TUNE"?

No they have nothing to do with the #define, they are only comments after closing braces.

I like to have, as a comment (with leading "//"), the statement or explanation of why a pair of braces was opened when i see the closing braces far away from the opening one.  It is just a source code reading help with no consequences :

}//anything behind the 1-line comment symbol ("//") blah blah...etc...

is equivalent to :

}

Also when I search for the end of a function within an editor, I can quickly find it when the function name is present as a comment like that, and when i have N closing braces in succession it helps me as well.

 

Posted by: @dorsay

3  The syntax of "#define" is "define constantName value" but you have "define TEST_TUNE" -  no value?

4 "#define is a useful C component that allows you to give a name to a constant value before the program is compiled."  That's according to the Arduino Reference PDF.  But why not just use "constant"?

#define is a pre-processor macro.  It defines a named symbol (TEST_TUNE in that case), and everywhere it appears in the code the symbol name will be replaced, before the actual compilation, by the value (none in that case) in the source code; the result of that will then be compiled.

If I remember well (but I'm not sure and I'll have to re-check), a defined value will be in program storage space, while a constant will be stored in RAM, which is much smaller on an Arduino.

You can use #define without a value and it just defines a named symbol with no content to replace it with.

We use this technique very often for management of options, or to prevent the double inclusion of header files (.h files).  Actually all .h files should be written with a "protecting" define enclosed in a "if not defined" block.  Example :

With something.h :

#ifndef SOMETHING
#define SOMETHING
...etc...something part 1...etc...
#include "someotherthing.h"
...etc...something part 2...etc...
#endif

and someotherthing.h :

#ifndef ANYTHING_ELSE
#define ANYTHING_ELSE
...etc...someotherthing part A...etc...
#include "something.h"
...etc...someotherthing part B...etc...
#endif

In this example, something.h needs someotherthing.h and vice versa.

So if your .ino sketch needs both :

#include "something.h"
#include "someotherthing.h"
...etc...main file...etc...

when the preprocessor reads first inclusion, since SOMETHING has not yet been defined, your code becomes :

#define SOMETHING     //<<<=== from something.h - SOMETHING symbol is now defined
...etc...something part 1...etc...
#include "someotherthing.h"
...etc...something part 2...etc...
#include "someotherthing.h" //<<== from the .ino, everything above this line is the first inclusion
...etc...main file...etc... //<<== from the .ino

(#ifndef, #ifdef, #else, #endif are removed by the preprocessor) 

Then the preprocessor has 2 inclusions for the same file someotherthing.h to process : for the first one, the ANYTHING_ELSE symbol has not yet been defined, so the #include line will be replaced with the content of the #ifndef...#endif block, but not for the 2nd inclusion because at this point ANYTHING_ELSE has been previously defined when the first inclusion happened.

So your code becomes :

#define SOMETHING //<<<=== from something.h - SOMETHING symbol is now defined
...etc...something part 1...etc...
#define ANYTHING_ELSE //<<<=== from someotherthing.h - ANYTHING_ELSE symbol is now defined
...etc...someotherthing part A...etc...
#include "something.h"
...etc...someotherthing part B...etc...
...etc...something part 2...etc...
// second #include "someotherthing.h" replaced with nothing because ANYTHING_ELSE is defined
...etc...main file...etc...

Similarly, the first inclusion of someotherthing.h brings in a new inclusion of something.h, but since the SOMETHING symbol has been previously defined, the #ifndef....#endif block prevents the contents of something.h to be re-included.

So the final source is :

#define SOMETHING //<<<=== SOMETHING symbol is now defined
...etc...something part 1...etc...
#define ANYTHING_ELSE //<<<=== ANYTHING_ELSE symbol is now defined
...etc...someotherthing part A...etc...
// #include "something.h" replaced with nothing because SOMETHING is defined
...etc...someotherthing part B...etc...
...etc...something part 2...etc...
// #include "someotherthing.h" replaced with nothing because ANYTHING_ELSE is defined
...etc...main file...etc...

Actually :

#define SOMETHING //<<<=== SOMETHING symbol is now defined
...etc...something part 1...etc...
#define ANYTHING_ELSE //<<<=== ANYTHING_ELSE symbol is now defined
...etc...someotherthing part A...etc...
...etc...someotherthing part B...etc...
...etc...something part 2...etc...
...etc...main file...etc...

 

For example in the sketch we have :

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

But actually the Adafruit_PMWServoDriver.h also has an inclusion of Wire.h.

Thankfully all of these header files are protected from multiple inclusion using this technique (TwoWire_h symbol for Wire.h, _ADAFRUIT_PWMServoDriver_H for the Adafruit header).

 

Eric


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @dorsay

it's after 10pm

and I'm going to have lunch here (France)

Eric


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

That's a fantastic explanation, Eric!  Just don't ask me to create anything like that! - lol

Posted by: @zeferby

I like to have, as a comment (with leading "//"), the statement or explanation of why a pair of braces was opened when i see the closing braces far away from the opening one.  It is just a source code reading help with no consequences :

That's a good idea!

I've found a debugger for Arduino on GitHub.  Don't know how good it will be or how difficult to implement/use.

https://github.com/anonymous-fablearn/ArduinoDebugger

On a personal note, I saw that you're in France from a post to John when talking about frogs.  Whereabouts in France?  You could say that my wife and I are Francophiles.  We stayed in Paris across the river from Notre Dame in Hotel Esmeralda in 1997 and then in 2008, we spent a month travelling all around France by train.  Amiens, Villers-Bretonneux, Bayeux, Rennes, Mont St Michel, Lyon, Avignon, Aix-en-Provence, Nice, Cinque Terre, Grenoble and Dijon.  Loved it.

David


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @dorsay

I've found a debugger for Arduino on GitHub.  Don't know how good it will be or how difficult to implement/use.

https://github.com/anonymous-fablearn/ArduinoDebugger

That looks promising ! I'll try this new toy ASAP...

Posted by: @dorsay

Whereabouts in France?

I'm near Orléans, about 120km south of Paris.

Eric


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

Posted by: @zeferby

That looks promising ! I'll try this new toy ASAP...

I'll be interested to hear what you think

Posted by: @zeferby

I'm near Orléans, about 120km south of Paris.

We flashed by in a TGV when travelling from Rennes to Lyon.

David


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@zeferby

I'm near Orléans, about 120km south of Paris.

That explains why you don't sail anymore!


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

@zeferby

Posted by: @zeferby

If I remember well (but I'm not sure and I'll have to re-check), a defined value will be in program storage space, while a constant will be stored in RAM, which is much smaller on an Arduino.

A constant is better to use for more technical reasons, but it costs memory (what ever type it is).  Using the #define(ed) pre-processor macro does not take up any program space on the chip at all.

Posted by: @zeferby

You can use #define without a value and it just defines a named symbol with no content to replace it with.

Indeed... many people often use it for debugging purposes:

#define DEBUG

Posted by: @zeferby

Actually all .h files should be written with a "protecting" define enclosed in a "if not defined" block.

Totally agree! - Although I didn't see you mention it (I'm sure you know this), but for the OP, these are called "include guards" or "inclusion guards".

Good job!
Here is a neat trick for you 🙂

void setup() {
  Serial.begin(9600);

  #if 0 // Acts as a multi line comment
     Serial.println("FOO"); // Will not print
     Serial.println("BAR"); // Will not print
     Serial.println("BAZ"); // Will not print
  #endif
 }

Cheers!


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

Sorry for dropping this conversation, I've been busy building panels for the switches and LEDS to go with the servos.  Nearly finished.  I've come across an Arduino Nano Shield that also fits on a Uno.  It has 16 sets of 3 digital pins and 8 sets of analog pins, gnd, v, and signal.  I'm now looking for some documentation about it .

David


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@dorsay

Do you have a picture or a link to the the place you purchased it ? anything written on the front/back silk screen of the board ?

Eric


   
ReplyQuote
(@dorsay)
Member
Joined: 4 years ago
Posts: 57
Topic starter  

@zeferby

Nothing written on the board front or back.  It was an eBay purchase

https://www.ebay.com.au/itm/Arduino-Nano-Shield-for-NANO-3-0-and-UNO-R3-shield-for-sevos-or-sensors/232997458970?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2060353.m2749.l2649

Other pictures on the eBay listing shows Deek Robot, so maybe that's the make although the one I have is slightly different.  I'd paste a picture of one but when I did, the picture was all over the text of the post

David


   
ReplyQuote
Page 3 / 9