I have a simple sketch to open a door when sensor (light sensor) is open and close if closed. Pin 10 stops closing (micro switch).
void setup() {
pinMode(9,OUTPUT ); //Brake
pinMode(12,OUTPUT ); //HIGH is forward
pinMode(8, INPUT); //light sensor
pinMode(10, INPUT); //micro swich
}
void loop() {
if (digitalRead(8)==HIGH && digitalRead(10)==LOW) {
digitalWrite(9, LOW);
digitalWrite(12, LOW);
analogWrite(3, 255);
}
else {
digitalWrite(9, HIGH);
}
if (digitalRead(8)==LOW && digitalRead(10)==LOW) {
digitalWrite(9, LOW);
digitalWrite(12, HIGH);
analogWrite(3, 255);
}
else {
digitalWrite(9, HIGH);
}
}
How check that light sensor is open/closed for a while (2 minutes) to avoid accidental initiation by mistake (light beam og shadow)?
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags
Save the value of millis() when the start action occurs and set a boolean to true to indicate that timing is taking place.
Each time through loop(), if the boolean is true then test whether the required period has elapsed by subtracting the start value from the current value of millis(). If so, act accordingly.
If the start action is cancelled before the period elapses then set the boolean to false
general tips:
Give all and every IO-Pin and all variables a selfexplaining name.
Give all functions a self-explaining name
This will make it much easier for you and of course all your potential helpers
to understand your code
There are some habits for the naming
variables start with a low-case-letter
So for example instead of adding comments like this
void setup() {
pinMode(9,OUTPUT ); //Brake
pinMode(12,OUTPUT ); //HIGH is forward
pinMode(8, INPUT); //light sensor
pinMode(10, INPUT); //micro swich
}
You can define constants and variables with self-explaining names
const byte Brake_Pin = 9;
const byte Direction_Pin = 12;
const byte lightSensor_Pin = 8;
const byte microSwitch_Pin = 10;
#define HOLD HIGH
#define LOOSE LOW
#define FORWARD HIGH
#define BACKWARD LOW
void setup() {
pinMode(Brake_Pin,OUTPUT );
pinMode(Direction_Pin,OUTPUT );
pinMode(lightSensor_Pin, INPUT);
pinMode(microSwitch_Pin, INPUT);
}
void loop() {
if (digitalRead(lightSensor_Pin) == HIGH && digitalRead(microSwitch_Pin) == LOW) {
digitalWrite(Brake_Pin, LOOSE);
digitalWrite(Direction_Pin, BACKWARD);
analogWrite(3, 255); // what is IO-pin 3 ????
}
else {
digitalWrite(Brake_Pin, HOLD);
}
best regards Stefan
1 Like
system
Closed
September 24, 2021, 7:19am
6
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.