Help to make things more fluid, controlling a buzzer and a led with an IR sensor

i am working with a sharp ir sensor that controls the brightness of a led and the Khz of a buzzer, but i feel that this is working a little bit slow, i will attach the code and i hope you can tell me where are the errors, so this sketch could be more fluid, the sketch works fine i just want to make the process faster

here is the code:

int IRpin = 1; //pin for the IR sensor
int pinLed = 9; // pin of the LED
float distance = 0; // variable of the distance between sensor and the objetc

void setup() {

Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode (pinLed, OUTPUT);
digitalWrite(pinLed, HIGH);

}

void loop() {

analogWrite(pinLed , distance); // using the variable distance to control the brightness of the led

float volts = analogRead(IRpin)0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
distance = 65
pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk

Serial.println(distance); // print the distance

delay(100); // arbitary wait time.

if (distance> 255){ // conditioning the values of distance
distance = 255;
}

if (distance<=30){
distance = distance - 20;
}

buzz(4, distance+30, 850); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
delay(10); // wait a bit between buzzes
}

void buzz(int targetPin, long frequency, long lenght){
long delayValue=1000000/frequency/2;
long numCycles = frequency*lenght/1000;

for( long i=0; i<numCycles;i++){

digitalWrite(targetPin,HIGH);
delayMicroseconds(delayValue);
digitalWrite(targetPin,LOW);
delayMicroseconds(delayValue);
}

}

Looks pretty good, but a few things:

  1. Take out your “arbitrary wait time.” That will slow everything down A LOT!
  2. Try to not use floats. They are slow. Instead, multiply by 5, then divide by 1024. You will get ~same answer, but much faster!

Good Luck!
baum

i fixed the code, and it works fine but, now that all works fine i cant note any change in the tone of the buzzer, here is the code:

int IRpin = 1; // analog pin for reading the IR sensor
int ledPin = 9; // Led Pin
int buzzer = 4; //buzzer pin
int distance = 0;

void setup() {
Serial.begin(9600);

pinMode(IRpin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void loop() {

float volts = analogRead(IRpin)0.00488; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65
pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance

if(distance<=19) { // this operation is for making the led turn off when it is in the proximity of 3 cm
distance = distance - 18;
}

analogWrite(ledPin, distance-20);
analogWrite(buzzer, distance+94);
}

i hope some of you can share your knowladge and help me out

thanks

I may be wrong... but the tone change in the buzzer is created by changing the frequency of the signal, not the duty cycle. And even then, it seems like you take away a third of the resolution of the PWM signal you output by adding 94 to it. :\

Also, by my calculations,

if AD == 1024 => volts = 5 = > distance = 11,07 => distance = -6,9 => ledPin = -26,9 => buzzer = 87,06 if AD == 0 => volts = 0 => distance = 0 => distance = -18 => ledPin = -38 => buzzer = 76. if AD == 512 => volts = 2,5 => distance = 23,7 => distance = 23,7 => ledPin =3,7 => buzzer = 117,7 So, why not:

if (distance >19)   analogWrite(ledPin, distance);
else   analogWrite(ledPin, 0);

Because that subtraction also affects what the PWM is going to be making it "non-linear". What you can see is that like this you will not use the entire scale of the PWM output. But as far as tone goes, you need to change frequency, not duty cycle. You may notice a difference in volume if you use the entire value range.

I may be wrong

No, you're not!

thanks to all of you :)