confusing error on complicated project

Hi all!
arduinofreaklol here!

I am making two arduino guns and a control back pack that are all programmed.
The gun (this applies to both guns) has a push button on the trigger and, when pressed, an LED in the barrel of the gun should light up and a speaker (i used an 8 ohm speaker instead of a piezo buzzer because it had better sound) should play a lazer sound. Also a permanently lit up light will be fixed onto the gun.

The control back pack has an Arduino Uno and a large bread board inside, so the wires from the guns connect to the back pack. The back pack really doesn’t do anything, but i did install a 7 segment display and button onto the back of it. The 7seg (7 segment display) should display a “1”. When the button is pressed, it should change to “2”. And then “3”, and so on to “9” then back to “1”. Everything was successful except the 7seg controlled by the button.

Here is the unfinished program:

const int button1 = 12;
const int led1 = 13;
const int lazer1 = 11;
const int button2 = 8;
const int led2 = 9;
const int lazer2 = 10;
const int a = 7;
const int b = 6;
const int c = 5;
const int d = 4;
const int e = 3;
const int f = 2;
const int g = 14;
const int dp = 15;
const int button3 = 16;
const int buzzer = 17;
const int flashlight1 = 18;
const int flashlight2 = 19;

void setup() {
for(int allpins = 2; allpins <= 19; allpins++) {
  pinMode(allpins, OUTPUT);
}

digitalWrite(flashlight1, HIGH);
digitalWrite(flashlight2, HIGH);
}

void loop() {
if(digitalRead(button1) == HIGH) {
  digitalWrite(led1, HIGH);
  LAZER_1();
} else {
  digitalWrite(led1, LOW);
  noTone(lazer1);
}

if(digitalRead(button2) == HIGH) {
  digitalWrite(led2, HIGH);
  LAZER_2();
} else {
  digitalWrite(led2, LOW);
  noTone(lazer2);
}

if(digitalRead(button3) == HIGH) {
  CHANGE_DIGITAL_NUMBER_BY_1();
}
}

void LAZER_1(void) {
tone(lazer1, 1900);
  delay(10);
  tone(lazer1, 1800);
  delay(10);
  tone(lazer1, 1700);
  delay(25);
  tone(lazer1, 1600);
  delay(25);
  tone(lazer1, 1500);
  delay(25);
  tone(lazer1, 1400);
  delay(25);
  tone(lazer1, 1300);
  delay(25);
  tone(lazer1, 1200);
  delay(25);
  tone(lazer1, 1100);
  delay(25);
  tone(lazer1, 1000);
  delay(25);
  tone(lazer1, 800);
  delay(5);
  tone(lazer1, 600);
  delay(5);
  tone(lazer1, 400);
  delay(5);
  tone(lazer1, 200);
  delay(3);
  tone(lazer1, 100);
  delay(3);
  tone(lazer1, 50);
  delay(1);
  tone(lazer1, 5);
  delay(1);
  tone(lazer1, 0);
  delay(25);
  noTone(lazer1);
}

void LAZER_2(void) {
tone(lazer2, 1900);
  delay(10);
  tone(lazer2, 1800);
  delay(10);
  tone(lazer2, 1700);
  delay(25);
  tone(lazer2, 1600);
  delay(25);
  tone(lazer2, 1500);
  delay(25);
  tone(lazer2, 1400);
  delay(25);
  tone(lazer2, 1300);
  delay(25);
  tone(lazer2, 1200);
  delay(25);
  tone(lazer2, 1100);
  delay(25);
  tone(lazer2, 1000);
  delay(25);
  tone(lazer2, 800);
  delay(5);
  tone(lazer2, 600);
  delay(5);
  tone(lazer2, 400);
  delay(5);
  tone(lazer2, 200);
  delay(3);
  tone(lazer2, 100);
  delay(3);
  tone(lazer2, 50);
  delay(1);
  tone(lazer2, 5);
  delay(1);
  tone(lazer2, 0);
  delay(25);
  noTone(lazer2);
}

void CHANGE_DIGITAL_NUMBER_BY_1(void) {
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(a, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dp, LOW);

if(digitalRead(button3) == HIGH) && (digitalRead(b) == HIGH) && (digitalRead(c) == HIGH) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}
}

Unfortunately, this unfinished program has an error! :frowning:

This is the error:

_2nd_arduino_nerf_gun_test.ino: In function ‘void CHANGE_DIGITAL_NUMBER_BY_1()’:
_2nd_arduino_nerf_gun_test:141: error: expected identifier before ‘(’ token
_2nd_arduino_nerf_gun_test:141: error: expected ‘;’ before ‘(’ token
expected identifier before ‘(’ token

How do i fix this and finish the program?

arduinofreaklol

 if(digitalRead(button3) == HIGH) && (digitalRead(b) == HIGH) && (digitalRead(c) == HIGH) {

Look carefully

(and did you see what I did with code tags there? You should do that)

 if(digitalRead(button3) == HIGH) && (digitalRead(b) == HIGH) && (digitalRead(c) == HIGH)

An if statement should have a set of parenthesis around the whole entire thing.

You've been asked numerous times to use code tags. Why do you keep ignoring that? Please help us to help you. Read this:

http://forum.arduino.cc/index.php/topic,148850.0.html

and pay particular attention to point 7. It makes it much easier for us to not only read your code but also to copy it into an editor to help look for your errors. It also prevents anything in your code from turning into italics or smileys. PLEASE read that post. PLEASE!!!!

Also
You set button1 as an output and then you try and read it!

Let's hope you have not wired it up correctly and are trying to push the button otherwise you are shorting the output and could damage it.

YAY!!!
I finally got the code look thing (whatever it is :slight_smile: to work!
Sorry for ignoring you Delta_G :frowning:
I am new to this and have been having problems :confused:
:slight_smile:

Did you fix the problem I pointed out in reply #1?
If so, what problems are you having now?

Ok!
I had another error and i fixed it by changing the test's code.
This program is still unfinished and has an error :frowning:

Here is the code:

const int button1 = 12;
const int led1 = 13;
const int lazer1 = 11;
const int button2 = 8;
const int led2 = 9;
const int lazer2 = 10;
const int a = 7;
const int b = 6;
const int c = 5;
const int d = 4;
const int e = 3;
const int f = 2;
const int g = 14;
const int dp = 15;
const int button3 = 16;
const int buzzer = 17;
const int flashlight1 = 18;
const int flashlight2 = 19;

void setup() {
  pinMode(button1, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(lazer1, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(led2, OUTPUT);
  pinMode(lazer2, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(flashlight1, OUTPUT);
  pinMode(flashlight2, OUTPUT);
  
  digitalWrite(flashlight1, HIGH);
  digitalWrite(flashlight2, HIGH);
}


void loop() {
  if(digitalRead(button1) == HIGH) {
    digitalWrite(led1, HIGH);
    LAZER_1();
  } else {
    digitalWrite(led1, LOW);
    noTone(lazer1);
  }
  
  if(digitalRead(button2) == HIGH) {
    digitalWrite(led2, HIGH);
    LAZER_2();
  } else {
    digitalWrite(led2, LOW);
    noTone(lazer2);
  }
  
  if(digitalRead(button3) == HIGH) {
    CHANGE_DIGITAL_NUMBER_BY_1();
  }
}

void LAZER_1(void) {
  tone(lazer1, 1900);
    delay(10);
    tone(lazer1, 1800);
    delay(10);
    tone(lazer1, 1700);
    delay(25);
    tone(lazer1, 1600);
    delay(25);
    tone(lazer1, 1500);
    delay(25);
    tone(lazer1, 1400);
    delay(25);
    tone(lazer1, 1300);
    delay(25);
    tone(lazer1, 1200);
    delay(25);
    tone(lazer1, 1100);
    delay(25);
    tone(lazer1, 1000);
    delay(25);
    tone(lazer1, 800);
    delay(5);
    tone(lazer1, 600);
    delay(5);
    tone(lazer1, 400);
    delay(5);
    tone(lazer1, 200);
    delay(3);
    tone(lazer1, 100);
    delay(3);
    tone(lazer1, 50);
    delay(1);
    tone(lazer1, 5);
    delay(1);
    tone(lazer1, 0);
    delay(25);
    noTone(lazer1);
}

void LAZER_2(void) {
  tone(lazer2, 1900);
    delay(10);
    tone(lazer2, 1800);
    delay(10);
    tone(lazer2, 1700);
    delay(25);
    tone(lazer2, 1600);
    delay(25);
    tone(lazer2, 1500);
    delay(25);
    tone(lazer2, 1400);
    delay(25);
    tone(lazer2, 1300);
    delay(25);
    tone(lazer2, 1200);
    delay(25);
    tone(lazer2, 1100);
    delay(25);
    tone(lazer2, 1000);
    delay(25);
    tone(lazer2, 800);
    delay(5);
    tone(lazer2, 600);
    delay(5);
    tone(lazer2, 400);
    delay(5);
    tone(lazer2, 200);
    delay(3);
    tone(lazer2, 100);
    delay(3);
    tone(lazer2, 50);
    delay(1);
    tone(lazer2, 5);
    delay(1);
    tone(lazer2, 0);
    delay(25);
    noTone(lazer2);
}

void CHANGE_DIGITAL_NUMBER_BY_1(void) {
  if(digitalRead(button3) == HIGH) {
    DIGITAL_1();
  }
  
  if((digitalRead(button3) == HIGH) && DIGITAL_1()) {
    DIGITAL_2();
  }
}

void(DIGITAL_1(void) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(dp, LOW);
}

void(DIGITAL_2(void) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(dp, LOW);
}
/[code]
The error is:

_2nd_arduino_nerf_gun_test.ino: In function 'void CHANGE_DIGITAL_NUMBER_BY_1()':
_2nd_arduino_nerf_gun_test:149: error: 'DIGITAL_1' was not declared in this scope
_2nd_arduino_nerf_gun_test:152: error: 'DIGITAL_1' was not declared in this scope
_2nd_arduino_nerf_gun_test:153: error: 'DIGITAL_2' was not declared in this scope
_2nd_arduino_nerf_gun_test.ino: At global scope:
_2nd_arduino_nerf_gun_test:157: error: expected ')' before '{' token
_2nd_arduino_nerf_gun_test:168: error: expected ')' before '{' token
'DIGITAL_1' was not declared in this scope

How do i fix this?

arduinofreaklol

This program is still unfinished and has an error

And we've got to guess what it is?

if((digitalRead(button3) == HIGH) && DIGITAL_1()) {

You told the compiler that DIGITAL_1 wasn't going to return anything (and it doesn't, which is good), but then you test the value that it doesn't return.

Am I right?

Hi,

I fixed it for you in with the minimum number of changes. Its not pretty but you should be able to understand it. When you have the sketch finished, you can come back if you want and we can show you how to make it better & shorter.

const int button1 = 12;
const int led1 = 13;
const int lazer1 = 11;
const int button2 = 8;
const int led2 = 9;
const int lazer2 = 10;
const int a = 7;
const int b = 6;
const int c = 5;
const int d = 4;
const int e = 3;
const int f = 2;
const int g = 14;
const int dp = 15;
const int button3 = 16;
const int buzzer = 17;
const int flashlight1 = 18;
const int flashlight2 = 19;
int DIGITAL_COUNT = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(lazer1, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(led2, OUTPUT);
  pinMode(lazer2, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(flashlight1, OUTPUT);
  pinMode(flashlight2, OUTPUT);
  
  digitalWrite(flashlight1, HIGH);
  digitalWrite(flashlight2, HIGH);
}


void loop() {
  if(digitalRead(button1) == HIGH) {
    digitalWrite(led1, HIGH);
    LAZER_1();
  } else {
    digitalWrite(led1, LOW);
    noTone(lazer1);
  }
  
  if(digitalRead(button2) == HIGH) {
    digitalWrite(led2, HIGH);
    LAZER_2();
  } else {
    digitalWrite(led2, LOW);
    noTone(lazer2);
  }
  
  if(digitalRead(button3) == HIGH) {
    CHANGE_DIGITAL_NUMBER_BY_1();
  }
}

void LAZER_1(void) {
  tone(lazer1, 1900);
    delay(10);
    tone(lazer1, 1800);
    delay(10);
    tone(lazer1, 1700);
    delay(25);
    tone(lazer1, 1600);
    delay(25);
    tone(lazer1, 1500);
    delay(25);
    tone(lazer1, 1400);
    delay(25);
    tone(lazer1, 1300);
    delay(25);
    tone(lazer1, 1200);
    delay(25);
    tone(lazer1, 1100);
    delay(25);
    tone(lazer1, 1000);
    delay(25);
    tone(lazer1, 800);
    delay(5);
    tone(lazer1, 600);
    delay(5);
    tone(lazer1, 400);
    delay(5);
    tone(lazer1, 200);
    delay(3);
    tone(lazer1, 100);
    delay(3);
    tone(lazer1, 50);
    delay(1);
    tone(lazer1, 5);
    delay(1);
    tone(lazer1, 0);
    delay(25);
    noTone(lazer1);
}

void LAZER_2(void) {
  tone(lazer2, 1900);
    delay(10);
    tone(lazer2, 1800);
    delay(10);
    tone(lazer2, 1700);
    delay(25);
    tone(lazer2, 1600);
    delay(25);
    tone(lazer2, 1500);
    delay(25);
    tone(lazer2, 1400);
    delay(25);
    tone(lazer2, 1300);
    delay(25);
    tone(lazer2, 1200);
    delay(25);
    tone(lazer2, 1100);
    delay(25);
    tone(lazer2, 1000);
    delay(25);
    tone(lazer2, 800);
    delay(5);
    tone(lazer2, 600);
    delay(5);
    tone(lazer2, 400);
    delay(5);
    tone(lazer2, 200);
    delay(3);
    tone(lazer2, 100);
    delay(3);
    tone(lazer2, 50);
    delay(1);
    tone(lazer2, 5);
    delay(1);
    tone(lazer2, 0);
    delay(25);
    noTone(lazer2);
}

void CHANGE_DIGITAL_NUMBER_BY_1(void) {
  if(digitalRead(button3) == HIGH && DIGITAL_COUNT == 0) {
    DIGITAL_1();
  }
  
  if((digitalRead(button3) == HIGH) && DIGITAL_COUNT == 1) {
    DIGITAL_2();
  }
}

void DIGITAL_1(void) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(dp, LOW);
    DIGITAL_COUNT = 1;
    }

void DIGITAL_2(void) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(dp, LOW);
    DIGITAL_COUNT = 2;
}

Paul

PaulRb, when i push the button the 7seg displays 2, and only 2.
I need it to display 1, then when the button is pressed, switch the 7seg digital up one digit until it reaches 9, and when the button is pushed and the 7seg displays 9, it should switch back to 1.

Hope you understand :slight_smile:

arduinofreaklol

But I only see functions for making a 1 or a 2.

Here is an example of some much more compact 7 segment code.

arduinofreaklol:
PaulRb, when i push the button the 7seg displays 2, and only 2...
Hope you understand :slight_smile:

Yes I understand. But don't start blaming all the shortcomings of your sketch on me!

I suspect that the problem is that the display is showing "1" but then immediately moving on to "2" much too fast to see. This is because what you want is to count up by only one when the button changes from low to high. Right now, it is only cheching that the button is high, without checking if it was low before.

so what do i do?

I altered the code so it looks like this:

const int button1 = 12;
const int led1 = 13;
const int lazer1 = 11;
const int button2 = 8;
const int led2 = 9;
const int lazer2 = 10;
const int a = 7;
const int b = 6;
const int c = 5;
const int d = 4;
const int e = 3;
const int f = 2;
const int g = 14;
const int dp = 15;
const int button3 = 16;
const int buzzer = 17;
const int flashlight1 = 18;
const int flashlight2 = 19;
int DIGITAL_COUNT = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(lazer1, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(led2, OUTPUT);
  pinMode(lazer2, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(flashlight1, OUTPUT);
  pinMode(flashlight2, OUTPUT);
  
  digitalWrite(flashlight1, HIGH);
  digitalWrite(flashlight2, HIGH);
}


void loop() {
  if(digitalRead(button1) == HIGH) {
    digitalWrite(led1, HIGH);
    LAZER_1();
  } else {
    digitalWrite(led1, LOW);
    noTone(lazer1);
  }
  
  if(digitalRead(button2) == HIGH) {
    digitalWrite(led2, HIGH);
    LAZER_2();
  } else {
    digitalWrite(led2, LOW);
    noTone(lazer2);
  }
  
  if(digitalRead(button3) == HIGH) {
    DIGITAL_COUNT += 1;
  }
  
  if((DIGITAL_COUNT) == 1) {
    DIGITAL_1();
  }
  
  if((DIGITAL_COUNT) == 2) {
    DIGITAL_2();
  }
}

void LAZER_1(void) {
  tone(lazer1, 1900);
    delay(10);
    tone(lazer1, 1800);
    delay(10);
    tone(lazer1, 1700);
    delay(25);
    tone(lazer1, 1600);
    delay(25);
    tone(lazer1, 1500);
    delay(25);
    tone(lazer1, 1400);
    delay(25);
    tone(lazer1, 1300);
    delay(25);
    tone(lazer1, 1200);
    delay(25);
    tone(lazer1, 1100);
    delay(25);
    tone(lazer1, 1000);
    delay(25);
    tone(lazer1, 800);
    delay(5);
    tone(lazer1, 600);
    delay(5);
    tone(lazer1, 400);
    delay(5);
    tone(lazer1, 200);
    delay(3);
    tone(lazer1, 100);
    delay(3);
    tone(lazer1, 50);
    delay(1);
    tone(lazer1, 5);
    delay(1);
    tone(lazer1, 0);
    delay(25);
    noTone(lazer1);
}

void LAZER_2(void) {
  tone(lazer2, 1900);
    delay(10);
    tone(lazer2, 1800);
    delay(10);
    tone(lazer2, 1700);
    delay(25);
    tone(lazer2, 1600);
    delay(25);
    tone(lazer2, 1500);
    delay(25);
    tone(lazer2, 1400);
    delay(25);
    tone(lazer2, 1300);
    delay(25);
    tone(lazer2, 1200);
    delay(25);
    tone(lazer2, 1100);
    delay(25);
    tone(lazer2, 1000);
    delay(25);
    tone(lazer2, 800);
    delay(5);
    tone(lazer2, 600);
    delay(5);
    tone(lazer2, 400);
    delay(5);
    tone(lazer2, 200);
    delay(3);
    tone(lazer2, 100);
    delay(3);
    tone(lazer2, 50);
    delay(1);
    tone(lazer2, 5);
    delay(1);
    tone(lazer2, 0);
    delay(25);
    noTone(lazer2);
}

void DIGITAL_1(void) {
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp, LOW);
}

void DIGITAL_2(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}
\[code]
This is a test, not the whole thing.

arduinofreaklol:
so what do i do?

Study the State Change example that comes with the IDE and learn to do what @PaulRB suggested you need to do.

i don't get it...i'm sorry.
:frowning:

i have no idea how to do this...

Not sure what we can do. We have told you what to do and pointed you to an example code of the technique.
What more do you want?
Brain transplants are not on offer.

arduinofreaklol:
i don't get it...i'm sorry.
:frowning:

i have no idea how to do this...

Keep studying it. Not the code itself, but the strategy of keeping track of the last state of the pin.