Forget the Arduino for now
Are you saying that wiring the LED the right way round in series with your resistor between 5V and GND lights the LED brightly and that the measured current that flows through the LED is 66 microAmps ?
Forget the Arduino for now
Are you saying that wiring the LED the right way round in series with your resistor between 5V and GND lights the LED brightly and that the measured current that flows through the LED is 66 microAmps ?
when I wire an the circuit without the microcontroller I got a reading in mA as you would expect. I just changed the const int of the LED pins to #define and am now reading 18.4mA which seems to be the correct reading.
I'm not sure what would cause that to happen?
#include <Servo.h> //Include servo library
#define WHITE_led 11 //assigning led to pin 11
#define GREEN_led 12 //assigning led to pin 12
// constants won't change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin
const int SERVO_PIN2 = 3; // Arduino pin connected to Servo Motor's pin
const int DISTANCE_THRESHOLD = 50; // threshold for sensor HIGH set to 50cm
rhather than
const int WHITE_led = 11;
const int GREEN_led = 12;
That's not making sense; for info, try adding
Serial.println(GREEN_led);
at the end of setup, to see what it's value is in both contexts.
It's fairly obvious that in one case, the value won't be correct, but what the value actually is may provide insight into what's going on. I suspect some form of collision between define values and your int naming, but it's hard to tell up front.
when add it the serial monitor prints 12
I did notice that the serial print for my sensor distance is the wrong way round, but that shouldn't have any affect on the LED's
Couldn't it be something included in the servo library causing it?
#include <Servo.h> //Include servo library
#define WHITE_led 11 //assigning led to pin 11
#define GREEN_led 12 //assigning led to pin 12
//constants won't change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin
const int SERVO_PIN2 = 3; // Arduino pin connected to Servo Motor's pin
const int DISTANCE_THRESHOLD = 50; // threshold for sensor HIGH set to 50cm
Servo servo; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// variables will change:
float duration_us, distance_cm; // Claryfying We want the ultrasonic sensor to disply units as cm
void setup() {
Serial.begin (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo2.attach(SERVO_PIN2); // attaches the servo on pin 3 to the servo object
servo.write(0); // gives a Value to the servo, giving it an initial position
servo2.write(-0); // gives a Value to the servo, giving it an initial position (- lets it know to move in the opposite direction)
pinMode(WHITE_led, OUTPUT); // set arduino pin to output mode
pinMode(GREEN_led, OUTPUT);
Serial.println(GREEN_led);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10); //wait 10 microseconds
digitalWrite(TRIG_PIN, LOW); //turn off ping
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
if(distance_cm < DISTANCE_THRESHOLD) //If Sensor is triggered
{
Serial.println ("Someone Is Infront Of the Sensor");
digitalWrite (GREEN_led, HIGH); //turn on GREEN LED
delay (500); // DELAY 1/2 second before turning green LED off
digitalWrite (WHITE_led, LOW);//turn off WHITE LED
}
else
{
Serial.println ("Nobody Is Infront Of the Sensor");
delay(2000);// 2 second delay before turning green LED on and white off
digitalWrite (GREEN_led, LOW); //turn off GREEN LED
digitalWrite (WHITE_led, HIGH); //turn ON WHITE LED
}
if(distance_cm < DISTANCE_THRESHOLD){ //IF Sensor is triggered
servo.write(90); // rotate servo motor to 90 degree
servo2.write(90); // rotate servo motor to 90 degree
delay(5000);// Stay opend for 5 seconds when sensor stops detecting something
}
else{
servo.write(0); // rotate back to original position
servo2.write(180); // rotate back to original position
}
// print the value to Serial Monitor
Serial.print("distance: "); //prints data as tecxt onto the serial port (prints 'distance:' before the distance mesured)
Serial.print(distance_cm); //prints data as text onto the serial port (prints the distance mesured in cm)
Serial.println(" cm"); //prints data as text onto the serial port (claryfies to print 'cm' at the end of distance)
delay(500); // delay before starting loop again
}
To clarify, so the value is 12 either way? Whether you use the define, or the int const? That's strange, because I was expecting that one of the two was resulting in a different value, and you therefore were not setting pinmode correctly in the one instance.
I'm out of time, and out of ideas; hopefully, someone else can step in. I'll be back in a few hours to see if there's been some progress.
Try this
byte ledPin = 12;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}
void loop()
{
}
Ugh, I have to wait 2 hours before posting again since my account is new.
Yes, I just changed it to double check and my current is measuring correctly rather than giving 66microAmps.
I assume that it was the fact my LED if statement was the wrong way round, although I am still slightly confused as to why it would give a false reading if that was the case?
if(distance_cm < DISTANCE_THRESHOLD) //If Sensor is triggered
{
Serial.println ("Someone Is Infront Of the Sensor");
digitalWrite (GREEN_led, HIGH); //turn on GREEN LED
delay (500); // DELAY 1/2 second before turning green LED off
digitalWrite (WHITE_led, LOW);//turn off WHITE LED
}
else
{
Serial.println ("Nobody Is Infront Of the Sensor");
delay(2000);// 2 second delay before turning green LED on and white off
digitalWrite (GREEN_led, LOW); //turn off GREEN LED
digitalWrite (WHITE_led, HIGH); //turn ON WHITE LED
}
rather than
if(distance_cm < DISTANCE_THRESHOLD)
{
Serial.println ("Nobody Is Infront Of the Sensor");
digitalWrite (WHITE_led, HIGH);
delay (500);
digitalWrite (GREEN_led, LOW);
}
else
{
Serial.println ("Someone Is Infront Of the Sensor");
delay(2000);
digitalWrite (WHITE_led, LOW);
digitalWrite (GREEN_led,HIGH);
}
You need trust level 1, you can get there by:
Users at trust level 1 can…
But does the real life circuit LED light up?
Can I suggest you use NewPing Library to read your ultrasonic unit, it makes code so much simpler.
Thanks.. Tom...
Yes, even when mesuring 66Microamps the LED's were bright, thats why I gathered the reading was incorrect.
I wasn't aware of that library, I'll have a look,
Thanks👍
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.