ISO C++ forbids converting a string constant to 'char*

Hello, I am trying to play an audio (mp3) from a SD card on an Arduino Mega 2560 (speaker 8 ohm). I dont understand the warning it's giving me, I have tried everything. Here is the whole error: C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino: In function 'void setup()':
C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino:21:20: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
tmrpcm.play("1.wav");

This is my code:

#include <SD.h>

#define SD_ChipSelectPin 4

#define SD_ChipSelect 10

#include <TMRpcm.h>

#include <SPI.h>

TMRpcm tmrpcm;

void setup() {

tmrpcm.speakerPin=9;

//pinMode(10,OUTPUT);

Serial.begin(9600);

if(!SD.begin(SD_ChipSelectPin))

{

Serial.println("SD fail");

return;

}

tmrpcm.setVolume(20);

tmrpcm.play("1.wav");

}

void loop() {

}

That's not an error.
The word is "warning" - read it again.

Warning for what? Because when everything is connected, the speaker doesn't work.

If the code uploads, it isn't an error, so must be something else is your problem

Please show diagnostic message in full

diagnostic message, meaning the code? Or the error? the full code is:

#include <SD.h>

#define SD_ChipSelectPin 4

#define SD_ChipSelect 10

#include <TMRpcm.h>

#include <SPI.h>

TMRpcm tmrpcm;

void setup() {

tmrpcm.speakerPin=9;

//pinMode(10,OUTPUT);

Serial.begin(9600);

if(!SD.begin(SD_ChipSelectPin))

{

Serial.println("SD fail");

return;

}

tmrpcm.setVolume(20);

tmrpcm.play("1.wav");

}

void loop() {

}

error:
C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino: In function 'void setup()':
C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino:21:20: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
tmrpcm.play("1.wav");

This error message is not complete

C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino: In function 'void setup()':
C:\Users\eleve\OneDrive\Documents\Arduino\speaker_code\sketch_jan26a\sketch_jan26a.ino:21:20: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
tmrpcm.play("1.wav");
^
Le croquis utilise 12832 octets (5%) de l'espace de stockage de programmes. Le maximum est de 253952 octets.
Les variables globales utilisent 1458 octets (17%) de mémoire dynamique, ce qui laisse 6734 octets pour les variables locales. Le maximum est de 8192 octets.

Thank you
The message means that the code compiled successfully despite the warning. Therefore your problem is elsewhere

could the problem be in the connection of the arduino and the speaker?
I connected from the SD card:
CS to 4
SCK to 13
MOSI to 11
MISO to 12
VCC to 5v
and both grn together

speaker to 9 and other grn

Do you see any messages in Serial after program start?

no, no message

Do the file 1.wav exist in the card?

yes, here

As far I see the filename is "effect(1).wav" and not the "1.wav"

Arduino port pins should never be directly connected to a speaker. You may have already destroyed the port pin.

Connect the port pin to an audio amplifier instead, with a capacitor and possible a voltage divider.

Have you tried to "cast"?

tmrpcm.play((char *)"1.wav");

I guess: "1.wav" is a constant, a string sitting in memory (Read-Only).
But the .play() uses a "Read-Write" (not const) pointer, e.g. as

play(char *);

It is a warning to tell you as: "your play could modify (write) your string, but the string comes from R/O-memory (e.g. flash) and cannot be written".
Forwarding a const to a non-const is always a warning (or even a bug! a warning can be a bug!)

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