Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******** SINK THE BATTLESHIP! *********
- This sketch is free to be copied and modified by anyone. There
- are obvious bugs in the program, but I'm too much of a noob
- to fix them.*/
- #include <Servo.h> // Include servo library for locking device
- #include <Keypad.h> // Include Keypad library to use keypad
- #include "pitches.h" //piezo buzzer attached to pin 12
- // notes in the "success" melody:
- int melody[] = {
- NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
- };
- // note durations: 4 = quarter note, 8 = eighth note, etc.:
- int noteDurations[] = {
- 4, 8, 8, 4, 4, 4, 4, 4
- };
- Servo myservo; // create servo object to control a servo lock
- // constants won't change. Used here to set a pin number :
- const int ledPin11 = 11; // number of the "Miss" LED pin
- const int ledPin10 = 10; // number of the "Hit" LED pin
- const byte ROWS = 4; // Four rows in keypad
- const byte COLS = 4; // Three columns in keypad
- int Hits = 0; // Initialize hit counter
- int Misses = 0; // Initialize miss counter
- char keys[ROWS][COLS] = // Define the Keymap
- {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = {9,8,7,6}; // Connect keypad to these Arduino pins.
- byte colPins[COLS] = {5,4,3,2}; // Connect keypad to these Arduino pins.
- unsigned int i=0,s,z=0;
- char name[3];
- Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(13); // attaches the servo on pin 13 to the servo object
- pinMode(ledPin10, OUTPUT); // set the Hit pin as output
- pinMode(ledPin11, OUTPUT); // set the Miss pin as output
- myservo.write(20); // Ensures servo is in locked position
- // Just some lights to let the player know game has started
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 1000, 500); // play a "Success" note on pin 12 for 1 Second
- delay(500);
- digitalWrite(11, LOW); // turn "Miss" lt off
- digitalWrite(10, HIGH); // turn "Hit" lt on
- tone(12, 800, 1000);
- delay(500);
- digitalWrite(10, LOW); // turn "Hit" lt off
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 1000, 500);
- delay(500);
- digitalWrite(10, HIGH); // turn "Miss" lt on
- digitalWrite(11, LOW); // turn "Miss" lt off
- tone(12, 800, 1000);
- delay(500);
- digitalWrite(10, LOW); // turn "Hit" lt off
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 1000, 500);
- delay(500);
- digitalWrite(10, HIGH); // turn "Miss" lt on
- digitalWrite(11, LOW); // turn "Miss" lt off
- tone(12, 800, 1000);
- delay(500);
- digitalWrite(10, LOW); // turn "Hit" lt off
- noTone(12);
- }
- void loop()
- {
- char key= kpd.getKey(); //continuous get a key-stroke
- if(key!=NO_KEY) // Check for a valid key.
- {
- switch (key)
- {
- case '*': { // TG-50 ajouté a cause de l'erreur crosses initialization of 'char num [s]'
- stop: s=i; //if * pressed than terminate and s becomes size of string
- char num[s]; //for calculation another string is formed of the size dynamically during run time by input string size
- Serial.println();
- name[i]='\0'; //terminate string with null character so it is easy to identify the termination of the string
- Serial.println("Le numéro entré est ");
- for(i=0;i<(sizeof(name));i++) //serially print the input string and copy it to the another string for calulation
- {
- Serial.print(name[i]);
- num[i]=name[i];
- //The following checks if the entry is a hit or a miss
- if (num[0] == 'C')
- {
- if (num[1] == '1')
- {
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- }
- if (num[0] == 'C') // C2 est le premier bloc du cuirassé TG-50
- {
- if (num[1] == '2')
- {
- Serial.println( "Atteint!");
- Hits = Hits + 1;
- digitalWrite(10, HIGH); // turn "Hit" lt on
- tone(12, 500, 500); // play a "Success" note on pin 12 for 1 Second
- delay(200);
- tone(12, 800, 1000);
- digitalWrite(10, LOW); // turn "Hit" lt off
- delay(500);
- }
- }
- if (num[0] == 'C') //C3 est le deuxième bloc du cuirassé TG-50
- {
- if (num[1] == '3')
- {
- Serial.println( "Atteint!");
- Hits = Hits + 1;
- digitalWrite(10, HIGH); // turn "Hit" lt on
- tone(12, 500, 500); // play a "Success" note on pin 12 for 1 Second
- delay(200);
- tone(12, 800, 1000);
- digitalWrite(10, LOW); // turn "Hit" lt off
- delay(500);
- }
- }
- if (num[0] == 'C') //C4 est le troisième bloc du cuirassé TG=50
- {
- if (num[1] == '4')
- {
- Serial.println( "Atteint!");
- Hits = Hits + 1;
- digitalWrite(10, HIGH); // turn "Hit" lt on
- tone(12, 500, 500); // play a "Success" note on pin 12 for 1 Second
- delay(200);
- tone(12, 800, 1000);
- digitalWrite(10, LOW); // turn "Hit" lt off
- delay(500);
- }
- }
- if (num[0] == 'C') //C5 est le quatrième bloc du cuirassé TG-50
- {
- if (num[1] == '5')
- {
- Serial.println( "Atteint!");
- Hits = Hits + 1;
- digitalWrite(10, HIGH); // turn "Hit" lt on
- tone(12, 500, 500); // play a "Success" note on pin 12 for 1 Second
- delay(200);
- tone(12, 800, 1000);
- digitalWrite(10, LOW); // turn "Hit" lt off
- delay(500);
- }
- }
- if (num[0] == 'C')
- {
- if (num[1] == '6')
- {
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- }
- if (num[0] == 'C')
- {
- if (num[1] == '7')
- {
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- }
- if (num[0] == 'C')
- {
- if (num[1] == '8')
- {
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- }
- if (num[0] == 'C')
- {
- if (num[1] == '9')
- {
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- }
- else
- { // This is what happens if the entry is a miss:
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 200, 300); // play a "Fail" note on pin 12 for 2 secs
- delay(300);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = Misses +1;
- }
- if (Hits == 8) // If there are four hits, the following happens:
- {
- delay(500);
- Serial.println("Vous avez coulé mon cuirassé!");
- Hits = 0;
- digitalWrite(10, HIGH); // turn "Hit" lt on
- for (int thisNote = 0; thisNote < 8; thisNote++) //play "Success!" melody
- {
- int noteDuration = 1000 / noteDurations[thisNote];
- tone(12, melody[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.30;
- delay(pauseBetweenNotes);
- noTone(12); // stop the melody
- }
- myservo.write(100); // Move servo To Open position
- delay(5000);
- myservo.write(20); // Move servo to Closed position after 5 seconds
- digitalWrite(10, LOW); // turn "Hit" lt off
- }
- if (Misses >= 32) // After 10 misses, the failure sequence starts:
- {
- Serial.print(" Plus de munition!");
- noTone(12);
- digitalWrite(11, HIGH); // turn "Miss" lt on
- tone(12, 500, 500); // play a "Fail" note on pin 12 for 2 secs
- delay(500);
- tone(12, 400, 500);
- delay(500);
- tone(12, 300, 500);
- delay(500);
- tone(12, 200, 500);
- delay(500);
- tone(12, 100, 500);
- delay(500);
- noTone(12);
- digitalWrite(11, LOW); // turn "Miss" lt off
- Misses = 0; // Resets the game for more fun!
- }
- }
- for(i=0;i<(sizeof(name));i++) //empty the string for the next turn use if it is not empty then new entered string will overlaped with old one
- {
- name[i]='\0';
- }
- i=0;
- z=0;
- z = atoi(num); //convert string to integer
- } //Ajouté à cause de l'erreur crosses initialization of 'char num [s]'
- break; //break the case'*'
- default: //programm comes here if key other than * is pressed
- if(i==3) //checks if keystroke is greater than 3
- {
- goto stop; //if it is greater than 3 then formation of making string stops
- }
- name[i]=key; //put key value to the string name
- i++; //increment string pointer for placing next key value else the pressed key will overwrite
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement