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);
}
}
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?
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.
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?
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);
}
}
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.
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");
}