hello
i am pretty green and a newbie in arduino coding, and most of this code is written with the help of google, so i really hope you can help me out here.
i have an arduino project with 2 diodes, an infrared distance sensor and a toggle switch. When i have the system in a mode where the diodes should light up, instead of turning on, they blink quite fast. i can see on the serial monitor that it changes between ex. mode 1 and 2 or 3 and 4. mode 1 makes the "upPin" led turn on until the distance sensor feels an obstacle, when the obstacle sensor feels an obstacle that is mode 2. when the switch is activated it goes to mode 3 and 4. when the obstacle sensor feels an obstacle it turns "downPin" led. when the obstacle is gone the led turns of.
the code looks like this.
#define SWITCH_PIN 13 // this is the toggle switch i use
int upPin = 9; // this is led no.1
int downPin = 10; // this is led no.2
int obstaclePin = 3; // this is the obstacle sensor
void setup() {
pinMode(SWITCH_PIN, INPUT_PULLUP);
Serial.begin(9600);
pinMode(upPin, OUTPUT);
pinMode(downPin, OUTPUT);
pinMode(obstaclePin, INPUT);
}
void loop() {
digitalRead(SWITCH_PIN) ? doModeOneStuff() : doModeTwoStuff();
int hasObstacle = digitalRead(obstaclePin);
}
void doModeOneStuff() {
int hasObstacle = digitalRead(obstaclePin);
if (hasObstacle == HIGH){
Serial.println("Mode 1, kører op");
digitalWrite(upPin, HIGH);
digitalWrite(downPin, LOW);
}
if (hasObstacle == LOW);
Serial.println("Mode 2, stopper bord");
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
}
void doModeTwoStuff() {
int hasObstacle = digitalRead(obstaclePin);
if (hasObstacle == LOW) {
Serial.println("Mode 3, kører ned");
digitalWrite(upPin, LOW);
digitalWrite(downPin, HIGH);
}
if (hasObstacle == HIGH);
Serial.println("Mode 4, stopper bord");
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
}