Go Down

Topic: HC-SRO4 and photoresistor (Read 116 times) previous topic - next topic

Rado_tornado

I'm new to programming but i want to make with arduino UNO, distance sensor HC-SRO4, photoresistor and two buzzers a project. I want from the buzzer to change sound depending of the distance in front of the device and the other buzzer to change sound depending of the light. I can't understand how to multitask with arduino. Can someone help me with this?

I wired everything correctly and this is my code.

int valuesv;                           
int analogsv = A0;
#define trigPin 10
#define echoPin 11                             
void setup() {
  Serial.begin(9600); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);                         
}
void loop() {
  valuesv = analogRead(analogsv);               
  valuesv = map(valuesv, 0, 1023, 0, 255);       
  Serial.println(valuesv);         
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1; 
  int thispitch = map(distance, 0, 200, 1093, 22);   
  int Pitch = map(valuesv, 0, 1023, 0, 255);     
  tone(12, thispitch);
  tone(13, Pitch);                             
  delay(500);                                   
  noTone(13);         
  noTone(12);                           
}

-Standby

Your code is easy to understand but there is a lot "newbie" mistakes.


#Define always first.

Variable should be define at the beginning of the code, as global or local if you use functions. 

Quote
I can't understand how to multitask with arduino. Can someone help me with this?
The micro execute instructions of your code step by step at a frequency of 16Mhz, so don't worry about multitasking :)

Quote
I want from the buzzer to change sound depending of the distance in front of the device and the other buzzer to change sound depending of the light.
The task is similar you can create a function that change the frequency of the buzzer depending on a value X.
The Mind is like a parachute it works best when opened.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy