Declared Void Error

Dear Community,

I tried to write a program for my Arduino to check, which contact is pressed to do special things for each contact. Yesterday it worked quite good and today i tried to rewrite the code to get a better outcome but now i get the ,,variable or field declared void" although i was sure i wrote it like i wrote it yesterday. Internet research did not help me either. Can you see the problem?

int direc = 0;
int vor = 3;
int ruck = 4;
int neutral = 5;
int vor_lamp = 9;
int ruck_lamp = 8;
int dir = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(vor,INPUT);
  pinMode(ruck,INPUT);
  pinMode(vor_lamp,OUTPUT);
  pinMode(ruck_lamp,OUTPUT);
  pinMode(neutral,INPUT);
  
  
}

void loop() {
  // put your main code here, to run repeatedly:
  check_direction();
  dir = direc;
  printdirection(dir);

}


  int check_direction()
  {
    if (digitalRead(vor) == HIGH)
    {
      direc = 1;
      return direc;
    }
    if ( digitalRead(ruck) == HIGH)
    {
      direc = 2;
      return direc;
    }

    if(digitalRead(neutral) == HIGH)
    {
      direc = 3;
      return direc;
    }
    else
    {
      direc = 0;
      return direc;
    }
  }

  void printdirection(direc)
  {
    if (dir) == 1
    {
      digitalWrite(ruck_lamp,LOW);
      digitalWrite(vor_lamp,HIGH);
    }
    if(dir == 2)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,HIGH);
    }
    if(dir == 3)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_Lamp,LOW);
    }
    if(dir == 0)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,LOW);
    }

    else
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,LOW);
    }
  }
1 Like

Greetings,

I think this is wrong. You didn't declare the type of the parameter. And also in the function printdirection you don't use direc. Why did you write it as a parameter?

It should be

void printdirection(typeofyour_variable nameofyourvariable)

void printdirection(direc) Is not the proper way to declare a function.

What data type is direc supposed to be? Also, and this is very important, do not name the functions input variables the same name as global variables.

void printdirection(int _direc) would work better. Thanks for using code tags.
See:
C++ Functions (w3schools.com)

C++ Functions - Default Parameter Value (Optional Parameters) (w3schools.com)

C++ Functions - Multiple Parameters (w3schools.com)

Also, direc and dir are not the same thing, and then there's the issue of the "if" condition and its parentheses.

@benutzername1 pasted in the wrong section. I moved it here. Please take more care where you post in future.

1 Like

Thank you so much it worked now. Indeed i used the ,,int" before the variable in the last code yesterday but did not think about it this time. Sry for posting it to the wrong section i thought it fitted best to troubleshooting. Thank you very much for your help this far.

Next problem. On standard, the pin i declared vor_lamp is set HIGH i tried it with other pins as well after restarting every time the vor_lamp pin is set High but i really don't know from where in my code this would come from.

Edit post #7 and add your latest code, please.

After pinmode output is set, I typically set initial state of the pin in setup().

It is the same code indeed i just corrected the mistakes. In the setup() i can use digitalWrite(8,LOW); for example? I still don't know for what i would use the analog input and output do you have some ressources on that?

Except the troubleshooting and installation section specifically says " not for your project" and this post is about a project of yours.

1 Like

I am very sry did not read that correctly i will look more correctly in my further topics.

I am very confused. My code is presented at the end. For what i thought what my code would do it would start with everything on LOW and only if i Plug in Pin 2 for example it would send a signal to the lamp on pin 7 and if i cut the connection it would only return 3 and nothing would happen. But still the Lamp on 7 burns the whole time and the serial Monitor tells me, that pin 2 switches between 0 and 1 the whole time even if the switch is not connected to any of pin 2 3 and 4. Why is this like that i do not understand that.


int direc = 0;
int vor = 2;
int ruck = 3;
int neutral = 4;
int vor_lamp = 7;
int ruck_lamp = 8;
int dir = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(vor,INPUT);
  pinMode(ruck,INPUT);
  pinMode(vor_lamp,OUTPUT);
  pinMode(ruck_lamp,OUTPUT);
  pinMode(neutral,INPUT);
  digitalWrite(vor,LOW);
  digitalWrite(ruck,LOW);
  digitalWrite(neutral,LOW);
  digitalWrite(ruck_lamp,LOW);
  digitalWrite(vor_lamp,LOW);
  
  
  
}

void loop() {
  // put your main code here, to run repeatedly:
  check_direction();
  dir=direc;
  set_direction(dir);
  Serial.println(digitalRead(vor));

}


  int check_direction()
  {
    if (digitalRead(vor) == HIGH)
    {
      direc = 1;
      return direc;
    }
    if (digitalRead(ruck) == HIGH)
    {
      direc = 2;
      return direc;
    }

    if(digitalRead(neutral) == HIGH)
    {
      direc = 3;
      return direc;
    }
    else
    {
      direc = 3;
      return direc;
    }
  }

  void set_direction(int dir)
  {
    if (dir == 1)
    {
      digitalWrite(ruck_lamp,LOW);
      digitalWrite(vor_lamp,HIGH);
    }
    if(dir == 2)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,HIGH);
    }
    if(dir == 3)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,LOW);
    }
    if(dir == 0)
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,LOW);
    }

    else
    {
      digitalWrite(vor_lamp,LOW);
      digitalWrite(ruck_lamp,LOW);
    }
  }

I've no idea what the OP's issues is from the OP's explanation. Perhaps the OP can post images of the project and schematics.

Did the OP try using a different pin to see if the problem follows?

You have floating inputs if you do not connect anything to them.

Input switches should be wired between input and ground and the internal pull up resistors should be enabled
so not

but

pinMode(vor, INPUT_PULLUP);

A floating input can read either LOW or HIGH depending on what interference it is picking up. Note that you will read a HIGH for no button press and LOW for a button press. You may have to change your logic over to match this.

For a full discussion about input pins see
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

1 Like

Ok i'll try this does that mean, i do not have to use another resistor between the 5 Volt output the switch and the input pins?

Hello benutzername1
Take a view and get some ideas.

Many thanks to LarryD

Have a nice day and enjoy coding in C++.

1 Like

Hello benutzername1
Try and checkout the simplified sketch to your project needs.
The sketch is untested.

int direc = 0;
int vor = 3;
int ruck = 4;
int neutral = 5;
int vor_lamp = 9;
int ruck_lamp = 8;
int dir = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(vor, INPUT_PULLUP);
  pinMode(ruck, INPUT_PULLUP);
  pinMode(neutral, INPUT_PULLUP);
  pinMode(vor_lamp, OUTPUT);
  pinMode(ruck_lamp, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!digitalRead(vor)) digitalWrite(vor_lamp, HIGH), digitalWrite(ruck_lamp, LOW), Serial.println("vor");
  if (!digitalRead(ruck)) digitalWrite(vor_lamp, LOW), digitalWrite(ruck_lamp, HIGH), Serial.println("ruck");
  if (!digitalRead(neutral)) digitalWrite(vor_lamp, LOW), digitalWrite(ruck_lamp, LOW), Serial.println("neutral");
}

Have a nice day and enjoy coding in C++.

1 Like

Thank you very much. The ! means ,,not" like in python?

Operators in C and C++ - Wikipedia

Yes.
You can use the instruction "^1" too.

Correct.

1 Like