Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:27:46+00:00 2026-06-07T17:27:46+00:00

I’m baffled. I have an identical program being uploaded to two different Arduino boards.

  • 0

I’m baffled. I have an identical program being uploaded to two different Arduino boards. It’s in C++.

It’s a much larger program, but I’m cutting it down to only the problematic part. Basically, I have a “host” Arduino and a “rover” Arduino communicating wirelessly. There are multiple rover units, but the problem is only happening on one of them. The rovers have motors that need to be calibrated, so I have static variables in my Motor namespace to hold those calibration values. To prevent having to change these values in the source code, recompile and reupload every time I want to calibrate it, I’m using the wireless system to allow the host to send calibration values to the rover at runtime.

Here’s the problem: on one rover, the values aren’t being updated if I call the ChangeSpeed method, but they do get updated if I modify the variables directly.

Let me stress that it works fine on four out of five rovers. The problem is happening on exactly one rover. The code being uploaded to each rover is identical.

The following code is causing a problem:

Motor.h:

namespace Motor
{
    static unsigned char left_speed = 0;
    static unsigned char right_speed = 0;

    void ChangeSpeed(unsigned char, unsigned char);
}

Motor.cpp:

void Motor::ChangeSpeed(unsigned char l_speed, unsigned char r_speed)
{
    left_speed = l_speed;
    right_speed = r_speed; 

    soft.println("Change speed: " + String(left_speed) + ", " + String(right_speed));
}

Main.cpp:

void UpdateSpeedValuesBad(unsigned char l_speed, unsigned char r_speed)
{
    Motor::ChangeSpeed(l_speed, r_speed);
    soft.println("Motor write: " + String(l_speed) + ", " + String(r_speed));
}

void UpdateSpeedValuesGood(unsigned char l_speed, unsigned char r_speed)
{
    Motor::left_speed = l_speed;
    Motor::right_speed = r_speed;
    soft.println("Motor write: " + String(l_speed) + ", " + String(r_speed));
}

void ReturnSpeedValues()
{
    soft.println("Motor read: " + String(Motor::left_speed) + ", " + String(Motor::right_speed));
}

Case 1:

On the bad rover, the host invokes UpdateSpeedValuesBad(5, 5), and then invokes ReturnSpeedValues. The output is:

Change speed: 5, 5
Motor write: 5, 5
Motor read: 0, 0

Case 2:

On the bad rover, the host invokes UpdateSpeedValuesGood(5, 5), and then invokes ReturnSpeedValues. The output is:

Motor write: 5, 5
Motor read: 5, 5

Case 3:

On a good rover, the host invokes UpdateSpeedValuesBad(5, 5), and then invokes ReturnSpeedValues. The output is:

Change speed: 5, 5
Motor write: 5, 5
Motor read: 5, 5

Am I doing something fundamentally wrong? I come from a C# background so C++ is pretty alien to me. I have no idea if I’m doing something that has undefined behaviour.


Edit: If I shove everything into one single file, it works fine. It only fails once I split it up across a header file and a cpp file.

Main.cpp:

#include <SoftwareSerial.h>

SoftwareSerial soft(9, 10);

namespace Motor
{
  static int left_speed = 0;

  void ChangeSpeed(unsigned char);
}

void Motor::ChangeSpeed(unsigned char l_speed)
{
  left_speed = l_speed;
  soft.println("Change speed: " + String(left_speed));
}

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

  soft.println("Before: " + String(Motor::left_speed));

  Motor::ChangeSpeed(5);
  soft.println("Bad attempt: " + String(Motor::left_speed));

  Motor::left_speed = 5;
  soft.println("Good attempt: " + String(Motor::left_speed));
}

void loop()
{
}

Output:

Before: 0
Change speed: 5
Bad attempt: 5
Good attempt: 5

Edit 2: I dove into the assembly and found this for the bad case. It’s using different memory addresses based on whether I call ChangeSpeed or I update the values directly. Anyone know why that would be? Is it a compiler bug or is it not guaranteed that the addresses will be the same?

000000a8 <setup>:
{ 
    Motor::ChangeSpeed(5, 6);
  a8:   85 e0           ldi r24, 0x05   ; 5
  aa:   66 e0           ldi r22, 0x06   ; 6
  ac:   0e 94 5f 00     call    0xbe    ; 0xbe <_ZN5Motor11ChangeSpeedEhh>

    Motor::left_speed = 5;
  b0:   85 e0           ldi r24, 0x05   ; 5
  b2:   80 93 00 01     sts 0x0100, r24

    Motor::right_speed = 6;
  b6:   86 e0           ldi r24, 0x06   ; 6
  b8:   80 93 01 01     sts 0x0101, r24
}
  bc:   08 95           ret

000000be <_ZN5Motor11ChangeSpeedEhh>:

void Motor::ChangeSpeed( unsigned char l_speed, unsigned char r_speed )
{
    left_speed = l_speed;
  be:   80 93 02 01     sts 0x0102, r24
    right_speed = r_speed; 
  c2:   60 93 03 01     sts 0x0103, r22
  c6:   08 95           ret
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:27:48+00:00Added an answer on June 7, 2026 at 5:27 pm

    You should not make these variables static. A static global variable means the variable is local to the compilation unit (generally, the .cpp file that is being compiled) so if you have the static variable declared in a header file and include that header file in 3 different .cpp files that are compiled separately then you will have 3 independent versions of that variable, one for each .cpp file.

    Instead, in the header file declare them as

    namespace Motor {
      extern unsigned char left_speed;
      extern unsigned char right_speed;
    
      void ChangeSpeed(unsigned char, unsigned char);
    }
    

    This tells the compiler that some file will provide a definition for these variables and to use that common shared definition.

    Then, since the variables need to be defined exactly once (this is called the one definition rule) you should add the definition to Motor.cpp:

    unsigned char Motor::left_speed = 0;
    unsigned char Motor::right_speed = 0;
    

    I chose Motor.cpp to hold the definition since this is where the definition of the ChangeSpeed function is.

    In C++, the static keyword works much differently than in C#. It might be somewhat similar when used inside a class definition, but that is where the similarities end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.