Arduino Laser Shooting Game Project

Hi all, I am new to Arduino. Since some personal reason, I need to build a laser shooting game.
I would appreciate some help as I really stuck for several weeks.


When I plug in the 9V battery to the Arduino Mega board, the first servo can rotate normally after the photoresistor sense the red laser. The second one is also ok but the rotate speed is slower. When I try to test the third photoresistor, the LCD monitor blink and the first two servo work abnormally and the remaining servo motors do not rotate.

I guess my circuit is wrong? There is not enough power supply for both LCD and servo motor?

Thanks so much for helping.

Sorry for poor English.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define TOT_TARGETS 6
#define TOT_SENSORS 6
#define PIN_TARGET 7


Servo Targets[TOT_TARGETS];  //Servo
int targetSensor[] = {A0, A1, A2, A3, A4, A5};  // LDR Sensor
int readingSensor[TOT_SENSORS];
int targetTimer[] = {0, 0, 0, 0, 0, 0}; 
int score = 0;
int timer = 100;
unsigned long interval = 100; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.


void setup() {
  Serial.begin(9600);

  lcd.backlight();
  lcd.clear();
  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print("SCORE: 0");
  lcd.setCursor(0, 1);
  lcd.print("TIMER: 0");

 
for (int targetNumber = 0; targetNumber < TOT_TARGETS; targetNumber++)
  {
    Targets[targetNumber].attach(targetNumber + PIN_TARGET);
    Targets[targetNumber].write(0);
  }
}

void loop() {

  lcd.clear();
  lcd.setCursor(0, 0);  //(Column,Row)
  lcd.print("SCORE: ");
  lcd.setCursor(12, 0);
  lcd.print(score);
  lcd.setCursor(0, 1);  //(Column,Row)
  lcd.print("TIME: ");
  lcd.setCursor(12, 1);
  lcd.print(timer);

  unsigned long currentMillis = millis(); // grab current time

     for (int targetsensorNumber = 0; targetsensorNumber < TOT_SENSORS; targetsensorNumber++) {
      readingSensor[targetsensorNumber] = analogRead({(targetSensor[targetsensorNumber])});
      Serial.print("ready = ");
      Serial.println(readingSensor[0]);
    }
      
    previousMillis = millis();
  
  targetDown();
  villainTarget();
  ifZero();
  Countdowntimer();
  
}

void villainTarget(){

  int target = random(1,7);
  if (target == 1) {
    targetTimer[0]++;
    Targets[0].write(0);
    delay(20);
  } else if (target == 2) {
    targetTimer[1]++;
    Targets[1].write(0);
    delay(20);
  }else if (target == 3) {
    targetTimer[2]++;
    Targets[2].write(0);
    delay(20);
  }else if (target == 4) {
    targetTimer[3]++;
    Targets[3].write(0);
    delay(20);
  }else if (target == 5) {
    targetTimer[4]++;
    Targets[4].write(0);
    delay(20);
  }else if (target == 6) {
    targetTimer[5]++;
    Targets[5].write(0);
    delay(20);
  }
}

void targetDown() {

  if ((readingSensor[0]> 700)){
    Serial.print("down1 = ");
    Serial.println(readingSensor[0]);
    score++;
    Targets[0].write(90);
    delay(20);
  }
  if (readingSensor[1] > 700) {
    Serial.print("down2 = ");
    Serial.println(readingSensor[1]);
    score++;
    Targets[1].write(90);
    delay(100);
  }

  if (readingSensor[2] > 700) {
    Serial.print("down3 = ");
    Serial.println(readingSensor[2]);
    score++;
    Targets[2].write(90);
    delay(100);
  }
  if (readingSensor[3] > 700) {
    Serial.print("down4 = ");
    Serial.println(readingSensor[4]);
    score++;
    Targets[3].write(90);
    delay(100);
  }
  if (readingSensor[4] > 700) {
    Serial.print("down5 = ");
    Serial.println(readingSensor[5]);
    score++;
    Targets[4].write(90);
    delay(100);
  }
  if (readingSensor[5] > 700) {
    Serial.print("down6 = ");
    Serial.println(readingSensor[6]);
    score++;
    Targets[5].write(90);
    delay(100);
  }
}

void Countdowntimer() {
  timer--;
  delay(1000);
}

void ifZero() {
  if (timer == 0) {
    for (int targetNumber = 0; targetNumber < TOT_TARGETS; targetNumber++)
    {
      Targets[targetNumber].write(0);
    }
    lcd.clear();
    lcd.print(" YOUR SCORE: ");
    lcd.setCursor(12, 0);
    lcd.print(score);
    delay(10000);
    score=0;
    timer = 100;
  }
}

The power supply is overwhelmingly insufficient.
Your 9V smoke detector battery isn't enough to run even one servo, and probably use it for that project isn't recommend from anyone on the forum.

Unfortunately I can't see any sensor on your Fritzing drawing…. I’d highly recommend a schematic/ circuit diagram.

Without a good understanding of the circuit, it’s almost impossible to see how the software should work.

cheers.

probably... even if it's AA batteries - how much power do each servo require? do you have their spec?

And it's definitely not going to work if this is actually this type of 9V battery

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.