Invalid conversion from 'const bool*' to 'bool*' [-fpermissive]

Arduino IDE compiles code, but when i want to put it in tinkercard (to simulate Arduino) it shows erroe that I put in topic. How do I change code for 7-segment display?

#include <IRremote.h>

// CONFIG: MODIFY VALUES TO FIT PLAY STYLE
const int maxAmmo = 5; // maximum ammunition count (max is 9)
const int shootCooldown = 500; // interval between each shot (in ms)
const int reloadTime = 1600; // time it takes to reload the gun (in ms)
const int resetTime = 500; // time the reset button has to be pressed to reset (in ms)

byte triggerPin = 2;
byte reloadPin = 3;
byte resetPin = 13;
byte irPin = 9;
byte hitLedPin = 6;
byte buzzerPin = 7;
byte pinsSegments[7] = {A0, A1, A2, A3, A4, A5, 12};

//ir communication
IRsend irsend; // setup IR transmitter
const unsigned long irSendHex = 0xFFA25D; // define hex value to send

// arrays of digits for 7 segment display
const bool digits[10][7] = {
  {0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 1, 1, 1, 1},
  {0, 0, 1, 0, 0, 1, 0},
  {0, 0, 0, 0, 1, 1, 0},
  {1, 0, 0, 1, 1, 0, 0},
  {0, 1, 0, 0, 1, 0, 0},
  {0, 1, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 1, 1},
  {0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 1, 0, 0}
};

// reload animation on 7 segment display
const bool reloadAnimation[6][7] = {
  {0, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 1, 1, 1, 1},
  {1, 1, 0, 1, 1, 1, 1},
  {1, 1, 1, 0, 1, 1, 1},
  {1, 1, 1, 1, 0, 1, 1},
  {1, 1, 1, 1, 1, 0, 1}
};
const int animationLength = 6;
const int animationInterval = 100;

const int melodyLength = 2;
const int melodyInterval = 150;
// shoot sound effect
const int shootSoundStartTone = 1000;
const int shootSoundEndTone = 700;
const int shootSoundInterval = 1;

//other variables
int ammoCount = maxAmmo;
unsigned long lastShot = shootCooldown*-1;
unsigned long startedReload = reloadTime*-1;
bool reloading = false;
unsigned long startedResetting = 0;
bool resetting = false;
unsigned long millisStartedTone = 0;
bool playingMelody = false;
int currentToneIdx = 0;
bool playingShootSound = false;
unsigned long millisStartedFrame = 0;
int currentFrameIdx = 0;
bool hitIndicator = false;

// reset function
void(* reset) (void) = 0;

// function to display digits on the 7 segment display
void displaySegments(bool segments[7]) {
  for(byte seg=0; seg<7; seg+=1) {
    digitalWrite(pinsSegments[seg], segments[seg]);
  }
}

void setup()
{
  // setup pins
  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(reloadPin, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  pinMode(irPin, OUTPUT);
  pinMode(hitLedPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  for(byte i=0; i<7; i+=1) {
    pinMode(pinsSegments[i], OUTPUT);
  }

  // GAME SETUP
  displaySegments(digits[ammoCount]); // display ammo count
  digitalWrite(hitLedPin, HIGH); // turn on hit indicator LED

   // play startup sound effect
  int startTones[] = { 196, 262, 330, 392 };
  for(int i=0; i<4; i++)
  {
    tone(buzzerPin, startTones[i]);
    delay(200);
  }
  noTone(buzzerPin);
}

void loop()
{
  unsigned long currentMillis = millis();
    // display ammo count
  if(!reloading) {
    displaySegments(digits[ammoCount]);
  }
   // play sounds
  if (playingMelody) {
    if (currentMillis - millisStartedTone >= melodyInterval) { // we are playing a melody, check if we finished playing current tone
      if(currentToneIdx+1 == melodyLength){ // we finished playing a tone, check if we finished the melody
        // end of melody
        noTone(buzzerPin);
        playingMelody = false;
} else { // we haven't finished the melody
        // play next tone
        currentToneIdx++;
        millisStartedTone = currentMillis;
      }
    }
  } else if (playingShootSound) {
    if (currentMillis - millisStartedTone >= shootSoundInterval) {
      if(currentToneIdx <= shootSoundEndTone) {
        // end of melody
        noTone(buzzerPin);
        playingShootSound = false;
           
      } else {
        // play next tone
        currentToneIdx -= 1;
        millisStartedTone = currentMillis;
        tone(buzzerPin, currentToneIdx);
      }
    }
  }
 // play reload animation
  if (reloading) {
    
    if (currentMillis - millisStartedFrame >= animationInterval) { // we are playing reload animation, check if we finished playing current frame
      // play next frame
      currentFrameIdx++;
      // cycle to start if reached end of animation
      if (currentFrameIdx > animationLength-1) {
        currentFrameIdx = 0;
      }
      millisStartedFrame = currentMillis;
      displaySegments(reloadAnimation[currentFrameIdx]);
    }
  }
 // input: reset
  if (digitalRead(resetPin) == LOW) {
    if(resetting) { // check if we were already holding the reset button
      if(currentMillis - startedResetting >= resetTime) { // check if we held the reset button for enough time
        reset();
      }
    } else { // start a new timer when we press it
      startedResetting = currentMillis;
      resetting = true;
    }
  } else {
    resetting = false;
  }
  // input: shoot
  if (digitalRead(triggerPin) == LOW && ammoCount > 0 && !reloading && currentMillis - lastShot >= shootCooldown)
  {
    // SHOOT
    irsend.sendNEC(irSendHex, 32); // send IR
    
    ammoCount--;
    lastShot = currentMillis;
    
    // SHOOT SFX
    currentToneIdx = shootSoundStartTone;
    millisStartedTone = currentMillis;
    tone(buzzerPin, currentToneIdx);
    if(playingMelody) {
      playingMelody = false;
    }
    playingShootSound = true;
  }

  // input: reload
  if (digitalRead(reloadPin) == LOW && ammoCount < maxAmmo && !reloading) {
    reloading = true;
    startedReload = currentMillis;
    
    // RELOAD ANIMATION
    currentFrameIdx = 0;
    millisStartedFrame = currentMillis;
    displaySegments(reloadAnimation[currentFrameIdx]);
  }
  
  // reload gun
  if (reloading && currentMillis - startedReload >= reloadTime) {
    ammoCount = maxAmmo;
    reloading = false;
  }
}

I would suggest you encode your segment patterns into bytes. This avoids the need to pass array references to functions, and is also much more efficient. You can use the Arduino bitRead() function to read the individual bits from the encoded patterns.

For example

const byte digits[10] = {
  0b0000001,
  0b1001111,
  ...
  0b0000100
};
...
void displaySegments(byte segments) {
  for(byte seg=0; seg<7; seg+=1) {
    digitalWrite(pinsSegments[seg], bitRead(segments, 7 - seg));
  }
}

In which line?

I guess this will produce the same error message.

In function 'void setup()':
93:35: error: invalid conversion from 'const bool*' to 'bool*' [-fpermissive]
73:6: note: initializing argument 1 of 'void displaySegments(bool*)'
In function 'void loop()':
111:37: error: invalid conversion from 'const bool*' to 'bool*' [-fpermissive]
73:6: note: initializing argument 1 of 'void displaySegments(bool*)'
152:54: error: invalid conversion from 'const bool*' to 'bool*' [-fpermissive]
73:6: note: initializing argument 1 of 'void displaySegments(bool*)'
195:52: error: invalid conversion from 'const bool*' to 'bool*' [-fpermissive]
73:6: note: initializing argument 1 of 'void displaySegments(bool*)'

Change the function signature to

void displaySegments(const bool segments[7]) {

For example, there is

And the definition of that is

They're const: "no one can change these values". But for displaySegments, they were not const: "I might change these values". But you don't, so you can add the const and say, "I won't change these values" and the compiler won't try to make that "invalid conversion".

@qwertyu12343284 seems to have fixed this problem and opened a topic about another problem with the same code.

This requires that displaySegments() does not call another function with a non-const usage.

Thank you so much, now it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.