Hi there, whilst i have had an arduino for a while i am still new to programming and hardware is more my thing (yet i love to learn)
With that in mind, i started on a project that uses a TIP122 to operate a relay to bypass a switch depending on the proximity to an object using the input from an ultrasonic sensor.
The hardware is all sorted yet the code is causing me some issues, so i have tried to go back to a basic code (turning on the on board LED, depending on the proximity to an object.
can anyone offer a solution, to what i am sure is simple. yet has eluded me
thanks for your time and sorry if the answer is starring me in the face
/* Ping))) Sensor
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
* SIG connection of the TIP circuit attached to Pin 8
*/
// this constant won't change. It's the pin number
// of the sensor's output:
#include <NewPing.h>
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
int tip122Light = 13; // TIP122 lightstrip curcuit connected to pin
int val = 0;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimu range needed
long duration, distance; //Duration used to calculate distance
int warningZone1 = 10; // range in cm which is considered to be dangerous first cycle of the striplight
int warningZone2 = 7; // range in cm which is considered to be dangerous second cycle of the striplight
int warningZone3 = 5; // range in cm which is considered to be dangerous third cycle of the striplight
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(tip122Light, OUTPUT);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
// convert the time into a distance
Serial.println(distance); // placed to see if the senor is reading correctly
{
val = digitalRead(echoPin);
}
if(distance <= warningZone1);
{
digitalWrite(tip122Light, val); // if the distance from the sensor to the obstacal is equal to or less than the distance advised in warninZOne 1 then turn the light on
}
}