I am new to Arduino and coding in general. I have done a few tutorials and easy projects and I am now starting something of my own from example code.
Trying to achieve a proximity sensor that will detect when someone's glass is close, then trigger a pump to fill the glass.
I am using an Arduino Uno and a Sparkfun APDS 9960. I have not gotten to the actual output to the pump yet, but the serial monitor is behaving the way it should. My issue happens when I power the Uno down.
If I unplug the Uno and then plug it back in, the serial monitor stops reading my proximity values. Even when I resend my code it doesn't work anymore. BUT, if I send the example code from the APDS 9960 library "Proximity Sensor", and THEN resend my code, I can get the sensor to behave normally again.
Could anyone shed some light on why this is happening? I have included both my code and the Proximity Sensor Example.
I have tried replacing the sensor and the Uno to see if there were hardware issues/damage, and got the same repeatable results each time.
My Code:
#include <Arduino_APDS9960.h>
#include <Adafruit_APDS9960.h>
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// Constants won't change:
const int sensorData = 2; //APDS Sensor input
const int pumpControl = 9; // the pin that the pump is attached to
// Variables will change:
int proximityValue = 255; // Proximity data between 0 and 255
void setup() {
// initialize the button pin as a input:
pinMode(sensorData, INPUT_PULLUP);
// initialize the LED as an output:
// initialize serial communication:
pinMode(pumpControl, OUTPUT);
Serial.begin(9600);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("---------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - Proximity Pump"));
Serial.println(F("---------------------------------------"));
}
void loop() {
// read the sensor data input pin:
if (APDS.proximityAvailable()) {
// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
int proximityValue = APDS.readProximity();
// print value to the Serial Monitor
Serial.println(proximityValue);
// compare the buttonState to its previous state
if (proximityValue <= 100) {
// if the sensor reads something close
(pumpControl == HIGH);
// if the current state is HIGH then the pump turns on
Serial.println("on");
}
else {
// if the current state is LOW then the pump stays off
(pumpControl == LOW);
Serial.println("off");
}
}
// wait a bit before reading again
delay(200);
}
Proximity Sensor Example
/*
APDS9960 - Proximity Sensor
This example reads proximity data from the on-board APDS9960 sensor of the
Nano 33 BLE Sense and prints the proximity value to the Serial Monitor
every 100ms.
The circuit:
- Arduino Nano 33 BLE Sense
This example code is in the public domain.
*/
#include <Arduino_APDS9960.h>
void setup() {
Serial.begin(9600);
while (!Serial);
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
}
void loop() {
// check if a proximity reading is available
if (APDS.proximityAvailable()) {
// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
int proximity = APDS.readProximity();
// print value to the Serial Monitor
Serial.println(proximity);
}
// wait a bit before reading again
delay(100);
}
Thank you for your time.