Hello! I bought an uno and a sensor. I connected the sensor to the arduino and use just a power-supply (5V) to get data without connecting the uno board to pc(Standalone Operation). As com port is connected to PC everything works fine while by disconnecting things go wrong. Here is my code:
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint16_t ambient_light = 0;
uint16_t red_light = 0;
uint16_t green_light = 0;
uint16_t blue_light = 0;
int pushButton = 8;
void setup() {
pinMode(pushButton, OUTPUT);
if (apds.init()) {
apds.enableLightSensor(true);
}
delay(500);
}
void loop() {
// Read the light levels (ambient, red, green, blue)
if ( !apds.readAmbientLight(ambient_light) ||
!apds.readRedLight(red_light) ||
!apds.readGreenLight(green_light) ||
!apds.readBlueLight(blue_light) ) {
// Serial.println("Error reading light values");
} else {
}
// Wait 1 second before next reading
delay(1000);
if (red_light>green_light) {
digitalWrite(pushButton, HIGH);
delay(2000);
digitalWrite(pushButton, LOW);
delay(5000);
};
}
What can be wrong? Should I change power-supply to