Hi, i am using a proximity sensor... Its closed NPN.... when it senses i can see its red light turn on but this code is not printing anything in serial:
Wiring (brown in 5v, blue in GRD, black in pin 2) * all online tutorials say the same wiring.
#define inputPin 2
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(inputPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalRead(inputPin);
if (inputPin == LOW) {
Serial.println( "None" );
delay(1000);
}
if (inputPin == HIGH) {
Serial.println( "Detected" );
delay(1000);
}
}
Thanks, i just had to switch your high to low. and it didnt need the last line - i commented it. It is just frustrating that my original code didnt work. But thank you, yours works.
I added comments to your original code that explain what your code does
.
if (inputPin == LOW) {
.
with your definition #define inputPin 2
this is the same as
if (2 == LOW) {
.
.
.
if (inputPin == HIGH) {
with your definition #define inputPin 2
this is the same as
if (2 == HIGH) {
kolaha showed the most compact version
your original code works if you use a variable that stores the IO-pin-state in this variable
#define inputPin 2
byte myIO_PinState; // variable to store the state of the IO-pin
void setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT);
}
void loop() {
myIO_PinState = digitalRead(inputPin); // store value of IO-pin 2 into variable named "myIO_PinState"
if (myIO_PinState == HIGH) {
Serial.println( "None" );
delay(1000); // blockate = disable code-execution for 1 second
}
if (myIO_PinState == LOW) {
Serial.println( "Detected" );
delay(1000); // blockate = disable code-execution for 1 second
}
}
delay() does blockate = disable code-exection for the specified time
a non-blocking way of doing things from time to time is shown in this code-version
How this works can be read here
#define inputPin 2
byte myIO_PinState; // variable to store the state of the IO-pin
unsigned long MyPrintTimer = 0; // Timer-variables MUST be of type unsigned long
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod ) {
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void setup() {
Serial.begin(9600);
Serial.println( F("Setup-Start") );
pinMode(inputPin, INPUT);
MyPrintTimer = millis(); // initialise timer-variable with actual snapshot of time
}
void loop() {
myIO_PinState = digitalRead(inputPin); // store value of IO-pin 2 into variable named "myIO_PinState"
if ( TimePeriodIsOver(MyPrintTimer, 1000) ) {
// if 1000 milliseconds time have passed by
// execute code below
if (myIO_PinState == HIGH) {
Serial.println( "None" );
}
else { // myIO_PinState has a different value than "HIGH"
Serial.println( "Detected" );
}
}
}
Be the change you want to see in the world
best regards Stefan