Newbie asking for help

This is not my code, i found it as part of a geocache description on instructables, but havent gotten anny help on there.... it's basicly hangman for arduino, but i can't get the game to chose a random word from defined list and then implement that into the game, maybe someone here sees the error and can help me??
Untitled_Project.ino:193:25: error: no member named 'getChars' in 'String'
secretWord = guessWord.getChars();

code part 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <TrueRandom.h>
#include <Servo.h>  //Needed to control servo
#include <LiquidCrystal.h>
#include <WString.h>
#include <Print.h>

#define POTPIN 3
#define SPEAKERPIN 5
#define BUTTONPIN 6
#define servon 3  //power to servo
#define LASTBUTTONSTATE HIGH
#define DEBOUNCEDELAY 50
#define NUMWORDS 10
#define button2 3 //button inside box

//define notes for buzzer
#define LOWNOTE 100
#define ALOW 440
#define CLOW 261
#define ELOW 329
#define FLOW 349
#define CHIGH 523
#define EHIGH 659
#define GHIGH 784
#define FSHIGH 740
#define AHIGH 880

void(* resetFunc) (void) = 0;             //declare reset function @ address 0

const char* words[] = {"ATMOSPHERE", "VERBATIM", "POSTSCRIPT", "DEADLINE", "CENSORSHIP", "SHORTHAND", "MONKEY", "DICKHEAD", "DILEMMA", "INTERFACE"};
const char letterVal[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char guessLetter;
//char guessWord;
char guessLast;
char guessed[25];
char* secretWord;
int guessedCount = 1;
int wordSize;
int gotOne = 0;
int alreadyGuessed = 0;
int showAsterisk = 0;
int buttonState;
int hangman = 0;
int totalRight = 0;
long lastDebounceTime = 0;
Servo lock;                   //create a servo object called lock
int button2on;                //integer for button inside box

LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10);
String guessWord = String(10);

// hangman graphic characters
byte topleft[] = { 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 };
byte topright[] = { 0x1C, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte bottomleft[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F, 0x1F, 0x1F };
byte bottomright[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte head[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x00, 0x00, 0x00 };
byte topbody[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x04, 0x04, 0x04 };
byte bottombody[] = { 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte rightarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x05, 0x06, 0x04 };
byte leftarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x15, 0x0E, 0x04 };
byte rightleg[] = { 0x04, 0x04, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00 };
byte leftleg[] = { 0x04, 0x04, 0x0A, 0x0A, 0x11, 0x00, 0x00, 0x00 };
byte leftarrow[] = { 0x10, 0x18, 0x1C, 0x1E, 0x1E, 0x1C, 0x18, 0x10 };
byte rightarrow[] = { 0x01, 0x03, 0x07, 0x0F, 0x0F, 0x07, 0x03, 0x01 };

void setup()
{
  
  lock.write(160);                //set lock servo to locked position before it is attatched to prevent servo from moving to 90 degrees on startup
  lock.attach(4);                 // Connected to servo to control lock
  Serial.begin(9600);
  pinMode (POTPIN, INPUT);
  pinMode (BUTTONPIN, INPUT);
  pinMode (SPEAKERPIN, OUTPUT);
//  pinMode (servon, OUTPUT);
  pinMode (button2, INPUT);
  
  lcd.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Guess the word");
  lcd.setCursor(1,1);
  lcd.print("to get inside!");
  delay(2000);
  
  // pick a random word using analog 5 for random data
  randomSeed(analogRead(5));
    newWord();
  draw_board();
}

  
  //draw the board
  //draw_board();
  
 //digitalWrite(servon, HIGH);
 //lock.write(90);
 //delay(500);
//digitalWrite(servon, LOW);
  
//}

void newWord(){
  
  //pick a random word from the list
  int pick = TrueRandom.random(NUMWORDS);
 const char* pickWord = words[pick];
  guessWord = pickWord;
 secretWord = guessWord.getChars();
 wordSize = guessWord.length();
  Serial.println(guessWord);          // print the word to serial for cheaters like me ;) ...or for testing purposes.
}

void draw_board(){
  
  // define the custom characters
  lcd.createChar(0, topleft);
  lcd.createChar(1, topright);
  lcd.createChar(2, bottomleft);
  lcd.createChar(3, bottomright);
  lcd.createChar(4, leftarrow);
  lcd.createChar(5, rightarrow);
  
  // draw blank hangman table
  lcd.clear();
  lcd.home();
  lcd.write((uint8_t)0);
  lcd.write(1);
  lcd.setCursor(0,1);
  lcd.write(2);
  lcd.write(3);
  
  // print underlines
  lcd.setCursor(3,1);
  for (int x=0; x < wordSize; x++){
    lcd.print("_");
  }
}

void loop(){

 //enable button to open lock from inside to allow the lid to be closed when the box is open
  button2on = digitalRead(button2);
  if (button2on == HIGH)
  {
       digitalWrite(servon, HIGH);   //apply power to servo
      lock.write(10);               //move servo to position 10 degrees
      delay(5000);                  //Hold door lock open for 5 seconds
      lock.write(160);              //move servo to position 160 degrees
      delay(1000);                  //wait for servo to catch up
      digitalWrite(servon, LOW);   //shut off power to servo
  }
  
  
    

 // letter selection via potentiometer
  int potVal = analogRead(POTPIN) / 35;      // 1024 / 26 ~= 39
  guessLetter = letterVal[potVal];
  
  // if letter is different from last, print to lcd
  // this prevents from printing the same char over and over
  if (guessLetter != guessLast){
    guessLast = guessLetter;
    showAsterisk = 0;
    
    // cycle through all guessed letters and determine whether to show * or the letter
    for (int x=0; x < guessedCount; x++){
      if (guessLetter == guessed[x]){
        showAsterisk = 1;
      }
    }
    // print letters to the left of selected letter
    lcd.setCursor(3,0);
    for (int x=5; x >= 1 ; x--){
      if (potVal - x >= 0){ 
        lcd.print(letterVal[potVal - x]);
      }
      else{
        lcd.print("|");
      }
    }
    
    // print left arrow
    lcd.write(4);
    
    // print the letter
    if (showAsterisk == 0){
      lcd.setCursor(9,0);
      lcd.print(guessLetter);
      alreadyGuessed = 0;
    }
    // print a *
    else{
      lcd.setCursor(9,0);
      lcd.print("*");
      alreadyGuessed = 1;
    }
        
    // print right arrow
    lcd.write(5);
    
    // print letters to the right of selected letter
    lcd.setCursor(11,0);
    for (int x=1; x <= 5 ; x++){
      if (potVal + x <= 25){ 
        lcd.print(letterVal[potVal + x]);
      }
      else{
        lcd.print("|");
      }
    }
  }
  
  
  // select button...debounced
  int reading = digitalRead(BUTTONPIN);
  
  // filter out noise and debounce
  if (reading != LASTBUTTONSTATE) {
    lastDebounceTime = millis();
  }
  
  // if select button is pressed, check word for selected letter
  if ((millis() - lastDebounceTime) > DEBOUNCEDELAY) {
    gotOne = 0;
    if (alreadyGuessed == 0){
      alreadyGuessed = 1;
      lcd.setCursor(9,0);
      lcd.print("*");
      for (int i = 0; i < wordSize; i++) {
        if (secretWord[i] == guessLetter) {
          lcd.setCursor(i+3,1);
          lcd.print(guessLetter);
          gotOne = 1;
          totalRight = totalRight + 1;
        }
      }
 
      // add letter to guessed letter array
      guessed[guessedCount] = guessLetter;
      guessedCount++;
      lastDebounceTime = millis();

      // none of the letters match, draw the next body part on the hangman
      if (gotOne == 0){
        buzz(LOWNOTE, 500);
        hangman++;
        draw_hangman(hangman);
      }
      else{
        // letter is in word, sound buzzer
        buzz(FSHIGH, 30);
        buzz(AHIGH, 50); 
      }
      
      //all letters have been guessed...WIN!
      if (totalRight == wordSize){
        gameOver(1);
      }
    }
    // this letter has already been guessed, sound buzzer
  }
}

Last edited on
code part 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
void draw_hangman(int var){
  
  switch (var){
    case 1:  
      lcd.createChar(1, head);           // head
      break;
    case 2:
      lcd.createChar(1, topbody);        // body
      lcd.createChar(3, bottombody);
      break;
    case 3:
      lcd.createChar(1, rightarm);       // right arm
      break;
    case 4:
      lcd.createChar(1, leftarm);        // left arm
      break;
    case 5:
      lcd.createChar(3, rightleg);       // right leg
      break;
    case 6:
      lcd.createChar(3, leftleg);        // left leg
      break;
    case 7:
      gameOver(0);
    default:
      break;
  }
}

void gameOver(int whatToDo){
  
  // decide whether win, lose or restart game  
  switch (whatToDo){
    case 0:  // GAME OVER
      lcd.clear();
      lcd.setCursor(6,0);
      lcd.print("ACCESS");
      lcd.setCursor(6,1);
      lcd.print("DENIED");
    
      //buzzer sound
      buzz(ELOW, 500);                  // GAME OVER!
      buzz(CLOW, 1000);                 // sound buzzer
      break;
    case 1:  // WINNER
    
          // buzzer sound
      buzz(ALOW, 150);
      buzz(CHIGH, 150);
      buzz(EHIGH, 150);
      buzz(AHIGH, 150);
      delay(150);
      buzz(GHIGH, 150);
      buzz(AHIGH, 500);
      
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print("ACCESS");
      lcd.setCursor(4,1);
      lcd.print("GRANTED");
     digitalWrite(servon, HIGH);    //apply power to servo
      lock.write(10);               //move servo to position 10 degrees
      delay(5000);                  // Hold door lock open for 5 seconds
      lock.write(160);              //move servo to position 160 degrees
      delay(1000);                  //wait for servo to catch up
  //    digitalWrite(servon, LOW);   //shut off power to servo
       
    

  }
  delay(2000);
  resetFunc();      // reset arduino for a new game
}

void buzz (int frequencyInHertz, long timeInMilliseconds){  
  Serial.println(frequencyInHertz);	 
    long delayAmount = (long)(1000000/frequencyInHertz);
    long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
    for (int x=0; x < loopTime; x++){ 	 
      digitalWrite(SPEAKERPIN, HIGH);
      delayMicroseconds(delayAmount);
      digitalWrite(SPEAKERPIN, LOW);
      delayMicroseconds(delayAmount);
    } 
    delay(20);
}
Last edited on
This thread should really be in the General C++ Programming forum. Thanks for using code tags, though!

I'm going to assume that you're using Adruino's strings here. Indeed, in Adruino strings, I don't see a member called getChars(). There is, however, c_str(), which appears to do what you want.

https://www.arduino.cc/en/Reference/CStr

-Albatross
Sorry my bad i thought i had it in the General C++ Programming forum, Can i or must a mod move it?
but if i replace secretWord = guessWord.getChars(); withsecretWord = guessWord.c_str(); i still get this error when trying to validate the code
Untitled_Project.ino:193:13: error: assigning to 'char *' from incompatible type 'const char *'
secretWord = guessWord.c_str();
so i can't transfer the script into my arduino.... :(
If secretWord never gets modified, you could always declare it as const char* and fix that issue.

-Albatross
but i thought that secretWord would get "modified" every time the game restart's and a new word get's picked from the word list...
I'm not that good at reading codes but I'm guessing that:
1
2
3
4
5
6
7
8
//pick a random word from the list
  int pick = TrueRandom.random(NUMWORDS);
 const char* pickWord = words[pick];
  guessWord = pickWord;
 secretWord = guessWord.getChars();
 wordSize = guessWord.length();
  Serial.println(guessWord);          // print the word to serial for cheaters like me ;) ...or for testing purposes.
}

Means that: first pick a random word from the list, then that word gets filtered into different definitions and in the end, the secret word line should tell the arduino wich word/letters I'm suposed to guess??
Correction. If the values pointed to by secretWord never get modified through dereferencing secretWord (which I don't think they do), then you could always declare it as const char* and fix that issue.

-Albatross
Took me some time to understand what you meant, but have now changed the beginning of the code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <Servo.h>  //Needed to control servo
#include <LiquidCrystal.h>
#include <WString.h>
#include <TrueRandom.h>

#define POTPIN 3
#define SPEAKERPIN 5
#define BUTTONPIN 6
#define servon 3  //power to servo
#define LASTBUTTONSTATE HIGH
#define DEBOUNCEDELAY 50
#define NUMWORDS 42
#define button2 3 //button inside box

//define notes for buzzer
#define LOWNOTE 100
#define ALOW 440
#define CLOW 261
#define ELOW 329
#define FLOW 349
#define CHIGH 523
#define EHIGH 659
#define GHIGH 784
#define FSHIGH 740
#define AHIGH 880

void(* resetFunc) (void) = 0;             //declare reset function @ address 0

const char* words[] = {"ab", "abb", "abbb", "abac", "abcd", "abcde",
                        "abcdes", "sadav", "fanon", "shintiyan", "avsdvas", "asdv", "nawab",
                       "avddv", "avdavs", "asdvasdv", "asdavae", "gamic", "avsad", "abvds",
                      "hygeia", "azygous", "bavin", "asdv", "asdvasdv", "bayadere",
                     "assegai", "homoousian", "pawky", "argrcvcvb", "abfdb", "adfbbadffg",
                       "hamose", "japanning", "canakin", "abfdbedh", "mazard", "abelectrolyze",
                       "agulpha", "amethodize", "hadji", "acequia"};
                     
const char letterVal[] = "abcdefghijklmnopqrstuvwxyz";
char guessLetter;
char guessLast;
char guessed[25];
const char* secretWord;
int guessedCount = 1;
int wordSize;
int gotOne = 0;
int alreadyGuessed = 0;
int showAsterisk = 0;
int buttonState;
int hangman = 0;
int totalRight = 0;
long lastDebounceTime = 0;
Servo lock;                   //create a servo object called lock
int button2on;                //integer for button inside box

LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10);
String guessWord = String(42);

// hangman graphic characters
byte topleft[] = { 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 };
byte topright[] = { 0x1C, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte bottomleft[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F, 0x1F, 0x1F };
byte bottomright[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte head[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x00, 0x00, 0x00 };
byte topbody[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x04, 0x04, 0x04 };
byte bottombody[] = { 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte rightarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x05, 0x06, 0x04 };
byte leftarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x15, 0x0E, 0x04 };
byte rightleg[] = { 0x04, 0x04, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00 };
byte leftleg[] = { 0x04, 0x04, 0x0A, 0x0A, 0x11, 0x00, 0x00, 0x00 };
byte leftarrow[] = { 0x10, 0x18, 0x1C, 0x1E, 0x1E, 0x1C, 0x18, 0x10 };
byte rightarrow[] = { 0x01, 0x03, 0x07, 0x0F, 0x0F, 0x07, 0x03, 0x01 };

void setup()
{
  
  lock.write(160);                //set lock servo to locked position before it is attatched to prevent servo from moving to 90 degrees on startup
  lock.attach(4);                 // Connected to servo to control lock
  Serial.begin(9600);
  pinMode (POTPIN, INPUT);
  pinMode (BUTTONPIN, INPUT);
  pinMode (SPEAKERPIN, OUTPUT);
//  pinMode (servon, OUTPUT);
  pinMode (button2, INPUT);
  
  lcd.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Guess the word");
  lcd.setCursor(1,1);
  lcd.print("to get inside!");
  delay(2000);
  
  // pick a random word using analog 5 for random data
  //randomSeed(analogRead(4));
  newWord();
  
  //draw the board
  draw_board();
  
// digitalWrite(servon, HIGH);
 lock.write(160);
 delay(1000);
// digitalWrite(servon, LOW);
  
}

void newWord(){
  
  //pick a random word from the list
  int pick = TrueRandom.random(NUMWORDS);
  const char* pickWord = words[pick];
  guessWord = pickWord;
  secretWord = guessWord.c_str();
  wordSize = guessWord.length();
  Serial.println(guessWord);          // print the word to serial for cheaters like me ;) ...or for testing purposes.
}

And now everything works... :D thanks alot for all the help..... :D
Topic archived. No new replies allowed.