#define echoPin 8
#define trigPin 7
#include <PCM.h>
int distance;
int return_duration;
long random_number;
int not_detected;
int detected;
@
const unsigned char sample[] PROGMEM = {
123, 123, 432, 566
@
};
long duration;
int read_sonar() { //in cm
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return_duration = duration * 0.034 / 2;
}
void forward(int duration, int turnRight, int turnLeft) {
analogWrite(11, turnLeft);
digitalWrite(10, LOW);
analogWrite(6, turnRight);
digitalWrite(5, LOW);
delay(duration);
}
void backward(int duration, int turnRight, int turnLeft) {
analogWrite(10, turnLeft);
digitalWrite(11, LOW);
analogWrite(5, turnRight);
digitalWrite(6, LOW);
delay(duration);
}
void off(int duration) {
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
}
void loop() {
Serial.begin(9600);
//tone(3, 250);
//delay(10000);
read_sonar();
Serial.print("The distance is:");
Serial.println(return_duration);
delay(100);
if(return_duration >= 5)
{
@
//startPlayback(sample, sizeof(sample));
@
random_number = random(50, 250);
backward(2000,100,100);
};
if(return_duration >= 5) {
detected == true;
}
else not_detected == true;{
};
while(not_detected == true)
forward(10000, 200, 200);
}
Someone told me to tag my posts correctly and i will be doing this for sure in the future, but this project is very sontanelesely and I just need a fast answer while not having that much time. Hope you guys understad that, you are amazing!
I need code tags quickly...Hope you understand that, you are amazing!
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)
--
but because you tried as I see one back tick (you need 3 for code tags) here is a link for you to study:
thanks for the tags!
see the link in my previous answer for what PROGMEM does
Yes, thank you for your help so much!
have fun
The maximum value for an unsigned char is 255, so the last 2 values in this array are invalid.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.