Tried this:
if (vcnl.readAmbient() < 100);
TinyServo.write(90);
digitalWrite(ledPin, HIGH);
{
TinyServo.write(70);
digitalWrite(ledPin, LOW);//
}
Led did not come on when I put my hand over the sensor. : (
Where did I go wrong?
/* Created by Nick Sebring: November 2017
This sketch is designed to activate a barcode reader by moving a chart in front of
the VCNL4010 I2C Proximity sensor. Thus triggering the servo to move a magnet over the sensor
in the handle of the barcode reader, activating the barcode readers infared scanner. Thus avoiding
to reach across my desk and having to pull the trigger. How lazy can us humans be? Hahahaha
The SCL and SDA are connected to A4 & 5 of my Arduino Uno
After getting this to work, it's being ported to a trinket. So, I'll have to ditch the "serial".
*/
#include <Wire.h>
#include "Adafruit_VCNL4010.h"
#include <Adafruit_SoftServo.h>
Adafruit_VCNL4010 vcnl;
Adafruit_SoftServo TinyServo;// Name servo instead of servo1,2 etc.
//#define TinyServo 7// Creates Servo object for magnet
int pos = 0; // Variable to store the servo position
int a = 0; //just a variable to check the current value of sensePin
// Creating speed settings for the servos sort of like PWM
int servoDelay1 = 25; // Creates servo delay speed=25 in miliseconds
int servoDelay2 = 50; // Creates servo delay speed=50 in miliseconds
int servoDelay3 = 150; // Creates servo delay speed=150 in miliseconds
const int ledPin = 7;// LED to notify servo sketch is working
//pinMode(8, INPUT_PULLUP);// CONNECT Servo to pin *
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);// Set led off at start-up
Serial.begin(9600);
Serial.println("VCNL4010 test");
if (! vcnl.begin()) {
Serial.println("Sensor not found :(");
while (1);
}
Serial.println("Found VCNL4010");
Wire.begin();
TinyServo.attach(7); // attaches the servo on pin 7 to activate barcode reader
TinyServo.write(90); // SET THE INITIAL SERVO POSITION AT POWER-UP
}
void loop() {
/* I know I have to somehow read the sensor data and convert that to an int, store the int and then
Move/sweep the servo arm based on the value of int.
*/
if (vcnl.readAmbient() < 100);
TinyServo.write(90);
digitalWrite(ledPin, HIGH);
{
TinyServo.write(70);
digitalWrite(ledPin, LOW);//
}
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
delay(200);
}