Offline
Newbie
Karma: 0
Posts: 7
|
 |
« on: October 13, 2012, 07:30:32 am » |
hi first, sorry for my English. I'm trying to mount a simulated bomb, when compiling I get the error "was not delcared key4 In This scope" Code many times I've looked and can not find the problem. /*Airsoft Bomb Version 1.1Creators: Chase Cooley&&&&&&&&&&&& Joey Meyer*/
#include <Keypad.h> #include <LiquidCrystal.h> #include <Tone.h> #define pound 14 Tone tone1;int Scount = 0; // count seconds int Mcount = 0; // count minutesint Hcount =0; // count hours int Dcount = 0; // count daysint val = 0; long secMillis = 0; // store last time for second addlong interval = 1000; // interval for seconds char password[4];int currentLength = 0; int i = 0;char entered[4]; int ledPin = 3; int ledPin2 = 4;//red lightint ledPin2 = 4; //green light
LiquidCrystal lcd(7,8,10,11,12,13);const byte ROWS = 4; //four rows const byte COLS = 3; //three columnschar keys[ROWS][COLS] = { char keys[ROWS][COLS] = { {'1','2','3'},{'4','5','6'}, {'7','8','9'},{'*','0','#'} }; byte rowPins[ROWS] = {18, 2, 14, 16}; //connect to the row pinouts of the keypadbyte colPins[COLS] = {17, 19, 15}; //connect to the column pinouts of the keypad byte colPins[COLS] = {17, 19, 15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as outputpinMode(ledPin2, OUTPUT); // sets the digital pin as output tone1.begin(9);lcd.begin(16, 2); Serial.begin(9600);lcd.clear(); lcd.setCursor(0,0);lcd.print("Enter Code: "); while (currentLength < 4){ lcd.setCursor(currentLength + 6, 1);lcd.cursor(); char key = keypad.getKey();key == NO_KEY; if (key != NO_KEY){ lcd.print(key);password[currentLength] = key; currentLength++;delay(200); }} if (currentLength == 4){ lcd.noCursor();lcd.clear(); lcd.home();lcd.print("You've Entered: "); lcd.setCursor(6,1);lcd.print(password[0]); lcd.print(password[1]);lcd.print(password[2]); lcd.print(password[3]);delay(3000); lcd.clear();currentLength = 0; }}
void loop(){ char key2 = keypad.getKey(); // get the keylcd.setCursor(0,0); timer();if (key2 != NO_KEY) {while (key2 == NO_KEY) {key2 = keypad.getKey(); }if (key2 != NO_KEY) {lcd.clear(); lcd.setCursor(0,0);lcd.print("Enter Code: "); while (currentLength < 4){ lcd.setCursor(currentLength + 6, 1);lcd.cursor(); char key2 = keypad.getKey();if (key2 != NO_KEY) {lcd.print(key2); entered[currentLength] = key2;currentLength++; delay(200);lcd.noCursor(); lcd.setCursor(currentLength + 5, 1);lcd.print("*"); lcd.setCursor(currentLength + 6, 1);lcd.cursor(); }} if (currentLength == 4){ if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3]){ lcd.noCursor();lcd.clear(); lcd.home();lcd.print(" Defused "); currentLength = 0;delay(2500); lcd.setCursor(0,1);lcd.print("Reset the Bomb"); }else {lcd.noCursor(); lcd.clear();lcd.home(); lcd.print(" Wrong ");lcd.setCursor(0,1); lcd.print(" Add - 1:30 ");if (Mcount < 14) {Mcount = Mcount + 1; }if (Scount < 59) {Scount = Scount + 30; }delay(1500); currentLength = 0;} }} }}
void timer(){ if ( Mcount >= 15 ){ while ( Mcount >= 15){ lcd.noCursor();lcd.clear(); lcd.home();lcd.print(" !BoOm! "); tone1.play(NOTE_A2, 200);digitalWrite(ledPin, HIGH); // sets the LED on tone1.play(NOTE_A2, 200);delay(10); // waits for a second digitalWrite(ledPin, LOW); // sets the LED offtone1.play(NOTE_A2, 200); delay(10); // waits for a seconddigitalWrite(ledPin2, HIGH); // sets the LED on tone1.play(NOTE_A2, 200);delay(10); // waits for a second digitalWrite(ledPin2, LOW); // sets the LED offtone1.play(NOTE_A2, 200); delay(10);// waits for a secondchar key4 = keypad.getKey(); if (key4 != NO_KEY){ while (key4 == NO_KEY){ key4 = keypad.getKey();} if (key4 = '#'){ lcd.clear();lcd.print("Reset the Bomb"); }} }if ( Mcount == 60) // if Mcount is 60 do this operation {//delay (32); // good place to fine tune timing Mcount = 0; // reset McountHcount ++; }if (Hcount> 23) {Dcount++; Hcount = 0; // have to reset Hcount to "0" after 24hrs}
lcd.setCursor (0,1); // sets cursor to 2nd linelcd.print ("Timer: "); lcd.print (Dcount);lcd.print (":"); lcd.print (Hcount);lcd.print (":"); lcd.print (Mcount);lcd.print (":"); lcd.print (Scount); if (Scount >59) // if 60 do this operation{ Mcount ++; // add 1 to McountScount = 0; // reset Scount //delay (58); // changes ms per min} if (Scount < 60) // do this oper. 59 times{ unsigned long currentMillis = millis();//delay (988); // changing by one = 60 ms a min if(currentMillis - secMillis > interval){ tone1.play(NOTE_G5, 200);secMillis = currentMillis; Scount ++; // add 1 to ScountdigitalWrite(ledPin2, HIGH); // sets the LED on delay(10); // waits for a seconddigitalWrite(ledPin2, LOW); // sets the LED off delay(10); // waits for a second} }}
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: October 13, 2012, 07:34:59 am » |
You haven't declared a variable called "key4". That's why the compiler is complaining.
I'm complaining about the format of your code, which the IDE's auto-format tool will help you sort out.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #2 on: October 13, 2012, 07:51:18 am » |
thanks for your quick response but the tool tells me that there are too many brackets left and I do not do anything. I copied the code of a web arduino and my level is still too low. I could help a little more in the solution? Thanks again and best regards.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: October 13, 2012, 07:56:38 am » |
Maybe a clue as to where you copied from?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #4 on: October 13, 2012, 08:00:09 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: October 13, 2012, 08:09:19 am » |
I've never been impressed with instructables. That one really sucks. I'll be back soon.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: October 13, 2012, 08:23:24 am » |
Nope, can't see where they get "key4" from. At least I can get it to format. /*Airsoft Bomb Version 1.1Creators: Chase Cooley&&&&&&&&&&&& Joey Meyer*/
#include <Keypad.h>#include <LiquidCrystal.h> #include <Tone.h> #define pound 14
Tone tone1; int Scount = 0; // count seconds int Mcount = 0; // count minutesint Hcount =0; // count hours int Dcount = 0; // count daysint val = 0; long secMillis = 0; // store last time for second addlong interval = 1000; // interval for seconds char password[4]; int currentLength = 0; int i = 0; char entered[4]; int ledPin = 3; //red lightint ledPin2 = 4; //green light
LiquidCrystal lcd(7,8,10,11,12,13); const byte ROWS = 4; //four rows const byte COLS = 3; //three columns char keys[ROWS][COLS] = { { '1','2','3' } ,{ '4','5','6' } , { '7','8','9' } ,{ '*','0','#' } }; byte rowPins[ROWS] = { 18, 2, 14, 16}; //connect to the row pinouts of the keypadbyte colPins[COLS] = {17, 19, 15}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as outputpinMode(ledPin2, OUTPUT); // sets the digital pin as output tone1.begin(9); lcd.begin(16, 2); Serial.begin(9600); lcd.clear(); lcd.setCursor(0,0); lcd.print("Enter Code: "); while (currentLength < 4){ lcd.setCursor(currentLength + 6, 1); lcd.cursor(); char key = keypad.getKey(); key == NO_KEY; if (key != NO_KEY){ lcd.print(key); password[currentLength] = key; currentLength++; delay(200); } } if (currentLength == 4){ lcd.noCursor(); lcd.clear(); lcd.home(); lcd.print("You've Entered: "); lcd.setCursor(6,1); lcd.print(password[0]); lcd.print(password[1]); lcd.print(password[2]); lcd.print(password[3]); delay(3000); lcd.clear(); currentLength = 0; } }
void loop(){ char key2 = keypad.getKey(); // get the keylcd.setCursor(0,0); timer(); if (key2 != NO_KEY) { while (key2 == NO_KEY) { key2 = keypad.getKey(); } if (key2 != NO_KEY) { lcd.clear(); lcd.setCursor(0,0); lcd.print("Enter Code: "); while (currentLength < 4){ lcd.setCursor(currentLength + 6, 1); lcd.cursor(); char key2 = keypad.getKey(); if (key2 != NO_KEY) { lcd.print(key2); entered[currentLength] = key2; currentLength++; delay(200); lcd.noCursor(); lcd.setCursor(currentLength + 5, 1); lcd.print("*"); lcd.setCursor(currentLength + 6, 1); lcd.cursor(); } } if (currentLength == 4){ if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3]){ lcd.noCursor(); lcd.clear(); lcd.home(); lcd.print(" Defused "); currentLength = 0; delay(2500); lcd.setCursor(0,1); lcd.print("Reset the Bomb"); } else { lcd.noCursor(); lcd.clear(); lcd.home(); lcd.print(" Wrong "); lcd.setCursor(0,1); lcd.print(" Add - 1:30 "); if (Mcount < 14) { Mcount = Mcount + 1; } if (Scount < 59) { Scount = Scount + 30; } delay(1500); currentLength = 0; } } } } }
void timer() { if ( Mcount >= 15 ){ while ( Mcount >= 15){ lcd.noCursor(); lcd.clear(); lcd.home(); lcd.print(" !BoOm! "); tone1.play(NOTE_A2, 200); digitalWrite(ledPin, HIGH); // sets the LED on tone1.play(NOTE_A2, 200); delay(10); // waits for a second digitalWrite(ledPin, LOW); // sets the LED offtone1.play(NOTE_A2, 200); delay(10); // waits for a seconddigitalWrite(ledPin2, HIGH); // sets the LED on tone1.play(NOTE_A2, 200); delay(10); // waits for a second digitalWrite(ledPin2, LOW); // sets the LED offtone1.play(NOTE_A2, 200); delay(10); // waits for a secondchar key4 = keypad.getKey(); if (key4 != NO_KEY){ while (key4 == NO_KEY){ key4 = keypad.getKey(); } if (key4 = '#'){ lcd.clear(); lcd.print("Reset the Bomb"); } } } if ( Mcount == 60) // if Mcount is 60 do this operation {//delay (32); // good place to fine tune timing Mcount = 0; // reset McountHcount ++; } if (Hcount> 23) { Dcount++; Hcount = 0; // have to reset Hcount to "0" after 24hrs}
lcd.setCursor (0,1); // sets cursor to 2nd linelcd.print ("Timer: "); lcd.print (Dcount); lcd.print (":"); lcd.print (Hcount); lcd.print (":"); lcd.print (Mcount); lcd.print (":"); lcd.print (Scount); if (Scount >59) // if 60 do this operation{ Mcount ++; // add 1 to McountScount = 0; // reset Scount //delay (58); // changes ms per min} if (Scount < 60) // do this oper. 59 times{ unsigned long currentMillis = millis();//delay (988); // changing by one = 60 ms a min if(currentMillis - secMillis > interval){ tone1.play(NOTE_G5, 200); secMillis = currentMillis; Scount ++; // add 1 to ScountdigitalWrite(ledPin2, HIGH); // sets the LED on delay(10); // waits for a seconddigitalWrite(ledPin2, LOW); // sets the LED off delay(10); // waits for a second} } } } }
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
|
 |
« Reply #7 on: October 13, 2012, 08:41:04 am » |
I grit my teeth every time I see code with the opening brace appended to the preceding line. Particularly when, as here, it's not done consistently.
One of the biggest problems novices face is understanding the control structure of there code and especially seeing where it is not what they intended. Putting each matching pair of { and } on separate lines indented by the same amount makes this much easier to check visually.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: October 13, 2012, 08:46:47 am » |
I grit my teeth every time I see code with the opening brace appended to the preceding line. K&R and Linux kernel standard, but whoever wrote that junk is no Linux system programmer. Braces on the wrong line were the least of my worries.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
|
 |
« Reply #9 on: October 13, 2012, 08:22:50 pm » |
K&R and Linux kernel standard
Yeah, but if you're programming on a VT100 then reducing the number of lines in your code is important. These days a line saved here or there is pretty irrelevant and the only justification I can see for that style is habit. Putting each { and } on a separate line and indented by the same amount roughly halves the amount of work needed to spot the matching pairs.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 16
Posts: 1036
Arduino rocks
|
 |
« Reply #10 on: October 14, 2012, 12:14:34 am » |
K&R and Linux kernel standard
Yeah, but if you're programming on a VT100 then reducing the number of lines in your code is important. These days a line saved here or there is pretty irrelevant and the only justification I can see for that style is habit. Putting each { and } on a separate line and indented by the same amount roughly halves the amount of work needed to spot the matching pairs. If you indent correctly, no work is needed to spot the matching pairs. Any good text editor should highlight matching parenheses/etc as well.
|
|
|
|
|
Logged
|
|
|
|
|
Anaheim CA.
Offline
Edison Member
Karma: 34
Posts: 2404
Experienced old Whitebeard with a Full head of Hair...
|
 |
« Reply #11 on: October 14, 2012, 12:19:54 am » |
That, I think was one of my most important discoveries, that and placing the cursor at the point of the curly brace to find the matching one. Now If I could only find a translator for the cuneiform output of the compiler error messages...
Bob
|
|
|
|
|
Logged
|
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36443
Seattle, WA USA
|
 |
« Reply #12 on: October 14, 2012, 06:57:14 am » |
Any good text editor should highlight matching parenheses/etc as well. That works OK if the matching brace/paren/square bracket is in the window. Vertical alignment works even when the curly brace is beyond the top or bottom edge of the screen. And, of course, no text editor can highlight the braces on the paper that comes out of the printer, while vertical alignment still works.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
|
 |
« Reply #13 on: October 14, 2012, 08:18:34 am » |
If you indent correctly, no work is needed to spot the matching pairs.
If you assume that the indentation and the braces match each other, that's true. But in this case you're effectively ignoring the braces and relying on the indentation. And the visual check that you need to perform to verify that the braces and the indentation match is roughly twice as difficult since you have to scan up the page to find which line the brace is expected to be on, and then across to the far end of the line to confirm it's there. And then do the same thing with all the intervening blocks to confirm that the brace you're looking at really is the matching pair of the one you started from. Relying on a smart code editor to find matching pairs for you is all very well, but only when you have that editor available. For example, most of the Arduino code I look at here, I look at online on a passive web page with no language support. Quite often it's so poorly laid out that I need to copy it out into an external editor to make any sense of it. In my day job I use a wide variety of editors in different environments and they don't all have support for bracket matching. Even when they do, it's a pain to have to do the mouse-move, click, see where the matching bracket was rather than just run your eye up the page to find it. It's better than nothing, but no substitute for getting the code layout right in the first place. There is a definite advantage to putting the matching pairs of braces on separate lines indented by the same amount. It makes it much easier to visual find and pair them up without requiring any tools support.
|
|
|
|
|
Logged
|
|
|
|
|
United Kingdom
Offline
Faraday Member
Karma: 146
Posts: 4887
|
 |
« Reply #14 on: October 14, 2012, 01:00:13 pm » |
hi first, sorry for my English. I'm trying to mount a simulated bomb, when compiling I get the error "was not delcared key4 In This scope" Code many times I've looked and can not find the problem.
There is a line: delay(10);// waits for a secondchar key4 = keypad.getKey();
I think that line should be split like this: delay(10);// waits for a second char key4 = keypad.getKey();
However, there are lots of other lines that look as if they have been joined together by mistake. btw the comment is wrong, delay(10) does not wait for a second.
|
|
|
|
« Last Edit: October 14, 2012, 01:04:14 pm by dc42 »
|
Logged
|
Formal verification of safety-critical software, software development, and electronic design and prototyping. http://www.eschertech.com
|
|
|
|
|