I need some help with my code!

Im building a package security system for a school project using a sensor, buzzer, three dual colored LED’s, and a 3 x 4 keypad. It’s a pretty basic system; the sensor will count how many times a package has been moved from the certain location and when it hits the desired number (2 in this case) the buzzer will sound and the LED’s will change from green to red. Once you type the correct passcode in the keypad (which is 0827) the system will reset and you will be able to take your package without worrying about sounding the alarm. I was able to upload my code to my ardunio with no problems, however, when I went to go test out my system, the passcode I wanted to use on the keypad wasn’t turning off the system. When I went to look back at the code, I was given this error message before it uploaded;

“/Users/Andy/Desktop/packageSecurity/packageSecurity.ino:4:38: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
Password password = Password( “0827” );”
^

Im not sure of how to fix this problem and was hoping i could find somebody on here who knows a little more about coding than I do. The code that I am using is;

#include <Password.h>
#define sensorPin1
#include <Keypad.h>
Password password = Password( “0827” );
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{‘1’, ‘2’, ‘3’},
{‘4’, ‘5’, ‘6’},
{‘7’, ‘8’, ‘9’},
{’*’, ‘0’, ‘#’},
};
byte rowPins[ROWS] = {11, 8, 5, 4};
byte colPins[COLS] = {3, 7, 2};
Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int sensorPin = 13;
const int buzzer1 = 12;
int led1 = 10;
int led2 = 9;
int led3 = 6;
int senseVal = 0;
int prevVal = 0;

void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(buzzer1, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
keypad.addEventListener(keypadEvent);
keypad.setDebounceTime(250);
}

void loop() {
keypad.getKey();
int senseState = digitalRead(sensorPin);
int preVal = senseState;
Serial.println(keypad.getKey());

if (senseState == HIGH) {
senseVal ++;
delay(1000);
}
if (senseState == HIGH && preVal != senseVal) {
senseVal = 1;
}
if (senseState == LOW && senseVal == 1) {
senseVal ++;
}
else {
}

if (senseVal >= 2) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
tone(buzzer1, 1000);
delay(2000);
noTone(buzzer1);
delay(2000);

}
if (senseVal < 2) {
noTone(buzzer1);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
}
}
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case ’ ': guessPassword(); break;
default:
password.append(eKey);
}
}}
void guessPassword(){
if (password.evaluate()){
noTone(buzzer1);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
senseVal = 0;
password.reset();
}
else{
password.reset();
}
}

Any help here would be appreciated! Thank you in advance

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Hi Tom!

At the moment, I don’t have access to the circuit I made. However, I’m almost positive that it’s wired right (I was able to get values for the keypad when I typed a number). If you want to me to make another post the proper way that is fine. I can also upload a schematic when I get home. I’m just wondering if you, or anybody, can decipher the error message I was given and how I can fix my code so that my project can run properly.

Thanks
Colin

Perhaps if you told us what this: "
Password password = Password( "0827" );".

is supposed to do, we could help.

Paul

Cooked_Sloth:
I’m just wondering if you, or anybody, can decipher the error message I was given […]

Sure enough Colin! Lets look at the message:

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 Password password = Password( "0827" );"

So it is a warning and not an error. It should compile fine. Next is the actual warning:

ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

The part in the brackets tell you this warning was issued, because the “write-strings” compiler option (-W is used for warning options) was enabled during compilation.

Now the actual message:

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

You get this warning, because the compiler is not allowed to convert the string constant (a string literal, everything in quotation marks) from the type const char[N] to the type char*. The ISO C++ standard used during compilation standard forbids this conversion.

Now, what does this mean for your code? You can find the reason for this warning, when you look at the Password’s class constructor. This only accepts parameters of type char*. With ISO C++ this should be const char* instead. If you don’t want to change the library code, then you can do a const cast, to cast away the const from the string literal.

Password password = Password( const_cast<char*>("0827") );