I'm trying to learn how to use I2C so that was the route I took.
So far I've got the sensor data to display on the serial monitor. The sensor is connected to A4,5.
That's analog. (?) I'm pretty sure it is Hence the A4, A5.
So, analogRead.A4 ?
int a = (analogRead.A4); ?
/* 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
//pinMode(8, INPUT_PULLUP);// CONNECT Servo to pin *
void setup() {
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.
*/
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
delay(200);
}
I2C is a synchronous serial communication protocol. Nothing analog about it. Here is a Nick Gammon tutorial. Here is a Sparkfun tutorial. Here is a library and example for the sensor.
marine_hm:
I'm trying to learn how to use I2C so that was the route I took.
So far I've got the sensor data to display on the serial monitor. The sensor is connected to A4,5.
That's analog. (?) I'm pretty sure it is Hence the A4, A5.
those pins are magically tied to I2C on your UNO (A4 is SDA, A5 is SCL).
so what is your question, then?
groundFungus:
I2C is a synchronous serial communication protocol. Nothing analog about it. Here is a Nick Gammon tutorial. Here is a library and example for the sensor.
Well, SHIT! Does that mean the Adafruit Trinket is OUT? I may have to re-think the housing of this to fit a trinket pro.
marine_hm:
Well, SHIT! Does that mean the Adafruit Trinket is OUT? I may have to re-think the housing of this to fit a trinket pro.
what does the spec sheet say for your trinket?
hint... it will work!
So then, I miss understood someone else a while back. Doesn't support Serial.print?
digital.Read (A4)? Let me read some more. I'll be back!
Copied from specs: Hardware I2C / SPI capability for breakout & sensor interfacing. Whew!
More reading! Thanks LOL
BTW; Happy Thanks Giving! Do you celebrate that over there?
marine_hm:
So then, I miss understood someone else a while back. Doesn't support Serial.print?
Trinket does not have a Serial port connection for debugging so the serial port monitor will not be able to send/receive data
did you not read the spec page?
you could look into SoftwareSerial if you are trying to connect something that speaks Serial...
Sweet! This compiled. Is there a better way of writing this?
if (vcnl.readAmbient() >100);
TinyServo.write(90);
{
TinyServo.write(70);
}
It's been close to 2 months since I ordered the servo. Should be here any day now. Then I can try it out. Don't have any servos just laying around.
marine_hm:
It's been close to 2 months since I ordered the servo. Should be here any day now. Then I can try it out. Don't have any servos just laying around.
Couldn't I just use an LED for now? Going to pull out the solderng iron. Burnt my hand last time plugging in an LED without the resistor. See, I CAN learn. : )
Oh wait; Even simpler. I'm using a breadboard. Duh! Be right back.
So you want to move the servo to 90 degrees then right away move it to 70 degrees? Why not just move it to 70 to start? What do you actually want to with the servo? The servo.write function does not wait for the servo to reach the new position, it just sends the signal and you must have a delay long enough for the servo to move to the new position before sending it to another position
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);
}
groundFungus:
For what?
Just to see if my sketch is working does something. Wave my hand over the sensor, turn the led on?
Should be pretty much the same as Move the servo to X position. No?
groundFungus:
So you want to move the servo to 90 degrees then right away move it to 70 degrees? Why not just move it to 70 to start? What do you actually want to with the servo? The servo.write function does not wait for the servo to reach the new position, it just sends the signal and you must have a delay long enough for the servo to move to the new position before sending it to another position
Ah! I see what you mean. For now I just want to see the servo move, then adjust the parameters to the meet desired range.
I want it to start at position X, then when triggered; move to position Y. Once the value returns within acceptable range move back to Position X
Another way of saying that is when darkness falls, turn on the led and when the sun comes up, turn it off?
if (vcnl.readAmbient() < 100);
TinyServo.write(90);
digitalWrite(ledPin, HIGH);
{
TinyServo.write(70);
digitalWrite(ledPin, LOW);//
}
First get rid of the ; (semi-colon) at the end of the if statement.
Then that says to move the servo and unconditionally turn the LED on if the light is below 100 then microseconds later turn the LED off. Put a delay after you turn the LED on and maybe you will be able to see it. I don't know what the curly brackets are for? Did you mean to stick an else in there?
if (vcnl.readAmbient() < 100)
{
TinyServo.write(90);
digitalWrite(ledPin, HIGH);
}
else
{
TinyServo.write(70);
digitalWrite(ledPin, LOW);//
}
Try this.
Yep. Thanks
if (vcnl.readAmbient() <= 100)
// TinyServo.write(100);
digitalWrite(ledPin, HIGH);
else
// TinyServo.write(30);
digitalWrite(ledPin, LOW);
delay (1000);
Entire sketch;
/* 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(30); // 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(100);
digitalWrite(ledPin, HIGH);
else
// TinyServo.write(30);
digitalWrite(ledPin, LOW);
delay (1000);
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
}
Look at my reply #15. You need the curly brackets to enclose the if and else blocks otherwise only the first statement after the if or else is executed conditionally. It works now, but without the brackets, when you uncomment the servo commands the LEDs won't work right. I always use curly brackets to enclose a block, even if it is only one line.
groundFungus:
Look at my reply #15. You need the curly brackets to enclose the if and else blocks otherwise only the first statement after the if or else is executed conditionally. It works now, but without the brackets, when you uncomment the servo commands the LEDs won't work right. I always use curly brackets to enclose a block, even if it is only one line.
Thank you! I thought earlier, you were implying they were not needed, so, I removed them.
Work in progress. Thanks again for all your help. Here and many other topics, that I've read but not actively participated in.
Well, I had everything working great on the Arduino Uno. Now I've converted it to work on a 5V Trinket.
Everything seems to work but the servo. The servo moves as the Trinket boots but nothing after.
/* 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 infrared 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 #0 & 2 of my 5V Trinket
*/
#include "Adafruit_SoftServo.h"
#include "Adafruit_VCNL4010.h"
#define SERVOPIN 4 // Servo control line (WHITE) on Trinket Pin #4
Adafruit_SoftServo TinyServo; //creates servo object
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 servos; NOT used in this sketch
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 = 1;// LED on pin 1 to notify servo sketch is working
Adafruit_VCNL4010 vcnl;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);// Set led off at start-up
TinyServo.attach(SERVOPIN); // attaches the servo
// Serial.begin(115200);
//Serial.println("VCNL4010 test");
if (! vcnl.begin()) {
//Serial.println("Sensor not found :(");
while (1);
}
}
void loop() {
if (vcnl.readProximity() > 2150)//2125 seems to be ideal without plastic cover
{
TinyServo.write(58);
digitalWrite(ledPin, HIGH);
}
else
{
TinyServo.write(25);
digitalWrite(ledPin, LOW);//
}
// Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
// Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
}
Do you see any reason the servo doesn't move when my hand is over the sensor?
Thanks.
Hmm. Just ran the Trinket Knob sketch from Adafruit. The Servo works just fine. Back to rewiring.
Putting this thread on hold. Getting help from Adafruit forum. No need to cross hairs.