What type of code do i need?
I'd looked for over the whole internet and the only thing i could find was the basic PING code
for an ultrasonic sensor with 3 output pins, but i have one with 4 (V3.2).
Thanks!
This is my recent code (the place where i thought i could fade the LED depending on the distance is colored orange).
// # Editor :Jiang from DFRobot
// # Data :18.09.2012
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version : 0.2
// # Description:
// # The Sketch for scanning 180 degree area 4-500cm detecting range
// # Connection:
// # Pin 1 VCC (URM V3.2) -> VCC (Arduino)
// # Pin 2 GND (URM V3.2) -> GND (Arduino)
// # Pin 4 PWM (URM V3.2) -> Pin 3 (Arduino)
// # Pin 6 COMP/TRIG (URM V3.2) -> Pin 5 (Arduino)
// #
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int URPWM = 3; // PWM Output 0?25000US?Every 50US represent 1cm
int URTRIG=5; // PWM trigger pin
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={
0x44,0x02,0xbb,0x01}; // distance measure command
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
PWM_Mode_Setup();
pinMode(led, OUTPUT);
}
void loop()
{
// set the brightness of pin 9:
analogWrite(led, brightness);
PWM_Mode();
Serial.println(Distance);
if(Distance<200) {
brightness = 5;
digitalWrite(led, HIGH);
}
if(Distance<150) {
brightness = 40;
digitalWrite(led, HIGH);
}
if(Distance<125) {
brightness = 120;
digitalWrite(led, HIGH);
}
if (Distance<100) {
brightness = 255;
digitalWrite(led, HIGH);
}
if (Distance>200) {
brightness = 0;
digitalWrite(led, LOW);
}
}
//PWM mode setup function
void PWM_Mode_Setup(){
pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRIG
digitalWrite(URTRIG,HIGH); // Set to HIGH
pinMode(URPWM, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd*);*
- }*
}
*void PWM_Mode(){ * - digitalWrite(URTRIG, LOW); // a low pull on pin COMP/TRIG triggering a sensor reading*
- digitalWrite(URTRIG, HIGH); // reading Pin PWM will output pulses*
- unsigned long DistanceMeasured=pulseIn(URPWM,LOW);*
- if(DistanceMeasured==80000){ // the reading is invalid.*
- Serial.print("Invalid"); *
- }*
- else{*
- Distance=DistanceMeasured/50; // every 50us low level stands for 1cm*
- }*
- Serial.print("Distance=");*
- Serial.print(Distance);*
- Serial.println("cm");*
}