Hii, I am new to this forum. I am currently making a code for LED based snake game with all leds attached from pin 13 to pin 2 in a line, when the snake hits end and user presses button 'a' the user scores, after score 10 the snake gets faster and faster it also gets slower if user looses lives, but I wanted to create a BOT (AI_BOT in game)
which when activated will play the game on it's own. To activate the bot I have used a function AIon which takes the input which is 'A' from user by serial monitor and then it again reads a different input from user, using Serial.read() function which is stored in variable NewAI (couldn't think of any other variable name) this variable will be passed into another function AI_BOT which is the actual bot, this function checks the value of the data passed into it and if it's 'w' it activates the bot and if 's' it deactivates it. The Problem is that user is supposed to type in 'Aw' to start bot but it takes multiple 'A's to do it like atleast three time 'AAAw' if I type this then it works otherwise it won't and it goes same with when I wanna deactivate the BOT. Also it skips through the function like wait(); if i include it in AIon function, it also skipping through a Serial.println("BOT activated" ) in function AI_BOT()
PLEASE Help me I have no idea what's going with that
int led[] = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
int sval[] = {0};
int counter = 0;
int LIVES = 5;
int score = 0;
int HIGHscore;
int j;
int q = 0;
int nm = 1;
int delaySpeed = 100;
int Time;
int NewAI;
int AIval = 0;
void setup() {
for(int i = 0; i < 12; i++){
pinMode(led[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
//For the speed of delay which tells how fast should led snake move
for(int i = 0; i < 10; i++){ //[[[[[MAIN CONTROL WHERE ALL FUNCTIONS FOR ANIMATIONS AND DECISIONS ARE EXECUTED]]
counter = i+3;
for(j = i; j < counter; j++){
digitalWrite(led[j], HIGH);
}
task(); //Task() determines if the user sent 'a' when the snake hit end
AIon(); //[[THIS IS WHERE I AM GETTING PROBLEM!! this reads value
// for the AI_BOT()
AI_BOT(NewAI); //The actual bot here with argument(NewAI)
delay(delaySpeed);
for(int u = i; u < counter; u++){
digitalWrite(led[u], LOW);
}
}
}
int task() { //Do not name any of the functions as "main" because this name doesn't work for function choose another name instead
if(Serial.read() == 'a'){
//task() function checks if the user pressed enter when the snakes reaches end
if(digitalRead(led[10]) == HIGH){ //Condition where the program checks if the user pressed enter if so, then executes the following code.
winAnimation(1);
score++;
Serial.print("LIVES LEFT = ");
Serial.print(LIVES);
Serial.print(" ");
Serial.print("TIME = ");
Serial.print(delaySpeed);
Serial.print(" ");
Serial.print("SCORE = ");
Serial.println(score);
if(score > 10){
HIGHscore = score;
delaySpeed -= 15;
}
delay(3);
if(Serial.read() == 'b'){
score += 20;
}
}
else if(digitalRead(led[10]) != HIGH){ //If user couldn't press enter at right time, then execute this code
LIVES--;
score--;
if((score < 0)){
//LIVES = 0;
score = score + (-1*score);
}
Serial.print("LIVES LEFT - ");
Serial.print(LIVES);
Serial.print(" ");
Serial.print("TIME = ");
Serial.print(delaySpeed);
Serial.print(" ");
Serial.print("SCORE = ");
Serial.println(score);
gameLost(4); //Function for animation
if(score > 13){
while(nm == 1){
Serial.print(" ");
Serial.println("[[///PERK UNLOCKED!!!////]]");
nm++;
}
delaySpeed += 30;
}
if(LIVES <= 0){
Looser(score);
}
}
}
}
int winAnimation(int val){ //blink(4) is a function inside a function named winAnimation(), winAnimation will be a common function for adding more animations to it, so wouldn't have to
switch (val){
case 0:
blink(4);
break; // make a new function, just add that function to this.
case 1:
AIanimation();
break;
}
}
int gameLost(int a){
int data = 0; //This function is for animation of the leds when player can't score
while(data < a){
for(int i = 6; i < 11; i++){
digitalWrite(led[i], LOW);
} //No current use of passing arguments or data in the function
for(int i = 0; i < 5; i++){
digitalWrite(led[i], HIGH);
}
delay(100);
for(int i = 0; i < 5; i++){
digitalWrite(led[i], LOW);
}
for(int i = 6; i < 11; i++){
digitalWrite(led[i], HIGH);
}
delay(100);
data++;
}
}
void blink(int x){
int limit = 0;
while(limit < x){ //Number of times this blink animaiton should run
for(int i = 0; i < 11; i++){
digitalWrite(led[i], HIGH);
}
delay(100);
for(int p = 0; p < 11; p++){
digitalWrite(led[p], LOW);
}
delay(100);
limit++;
}
}
int LOOSER(int scoredata){ //LOOSER() executes when player has lost all lives, it deals with things like if player wanna restart, revive, high score, number of chances for revive
Serial.println("[GAME LOST!!!]");
Serial.println("");
Serial.println("LIVES LEFT = 0");
Serial.println("Press 'q' to exit");
while(true){
if(Serial.read() == 'q'){
LIVES = 5;
delaySpeed = 100;
score = 0;
Serial.print("[HIGH SCORE] = ");
Serial.println(HIGHscore);
break;
}
if(q < 2){
if(scoredata > 10){
Serial.println("You have more than 10 points available");
Serial.println();
Serial.println("Press 't' to revive");
filter();
wait();
if(Serial.read() == 't'){
q++;
LIVES = 1;
delaySpeed += 60;
break;
}
delay(4);
if(Serial.read() == 'q'){
LIVES = 5;
delaySpeed = 100;
score = 0;
Serial.print("[HIGH SCORE] = ");
Serial.println(HIGHscore);
break;
}
}
}
if(q >= 2){
Serial.println("[[Can't revive you have used up two chances]]");
}
}
}
void filter(){ //Filters out the serial buffer
Serial.read();
}
void wait(){ //Hangs the control loop at specific point in code
while(Serial.available() == 0){
}
}
/***************************************THIS IS THE ACTUAL BOT BELOW HERE*/
int AI_BOT(char Z){
//AI BOT which plays the game, activates when player enters the command 'AAAw' and stops when PLYR enters 'AAAs'
if(Z == 'w'){
while(AIval < 1){
Serial.println("[[ ///BOT Activated\\\\ ]]");
AIval = 2;
}
if(digitalRead(led[10]) == HIGH){
winAnimation(1);
score++;
Serial.print("LIVES LEFT = ");
Serial.print(LIVES);
Serial.print(" "); //Need to improve system for, data sending system doesn't work.
Serial.print("TIME = ");
Serial.print(Optimizer(sval[0]));
Serial.print(" ");
Serial.print("SCORE = ");
Serial.println(score);
if(score > 10){
HIGHscore = score;
delaySpeed -= 15;
if(delaySpeed <= 0){
delaySpeed = delaySpeed + (-1*delaySpeed) + 4; //this line prevents delayspeed to get in -ve range
}
}
}
}
else if(Z == 'm'){
while(AIval == 2){
Serial.println("[[ /////Bot Deactivated\\\\\\]]"); //IT SKIPS THIS LINE AFTER
//USING TWICE OR MORE
AIval = 0;
}
}
}
void AIanimation(){
for(int i = 7; i < 11; i++){
digitalWrite(led[i], HIGH);
}
delay(100);
for(int i = 7; i < 11; i++){
digitalWrite(led[i], LOW);
}
}
/************************FUNCTION WHICH PASSES DATA OR VALUES TO AI_BOT*/
void AIon(){ //Takes the input to set value for variable responsible to turn on and turn off the AI bot.
if(Serial.read() == 'A'){
Serial.println("Do you wanna activate the bot? press 'w' or 's' otherwise");
wait();
if(Serial.available() > 0){
filter();
}
delay(3);
NewAI = Serial.read();
Serial.print(" ");
Serial.println(Serial.available());
}
}