I want to make a car that can stop when it detects obstacles and red lights, but I am still struggling with coding. Can anyone help me?
Please show what you have already achieved.
i have finished wiring the project, and I'm using the l293d motor shield and the color sensor TCS3200
Please don't use screenshots of the code, Insert the code as text.
Sorry
#include <AFMotor.h>
#define S0 12
#define S1 13
#define S2 9
#define S3 10
#define sensorOut 11
AF_DCMotor leftFront(1);
AF_DCMotor leftBack(2);
AF_DCMotor rightBack(3);
AF_DCMotor rightFront(4);
int trigPin1 = 6;
int echoPin1 = 7;
int frequency = 0;
void setup() {
Serial.begin(9600);
// color sensor
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
// ultrasonic sensor
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
// turn on motor 1
leftFront.setSpeed(200);
leftFront.run(RELEASE);
// turn on motor 2
leftBack.setSpeed(200);
leftBack.run(RELEASE);
// turn on motor 3
rightBack.setSpeed(200);
rightBack.run(RELEASE);
// turn on motor 4
rightFront.setSpeed(200);
rightFront.run(RELEASE);
}
int i;
void loop() {
// redlight function
digitalWrite(S2, LOW);
digitalWrite(S3,LOW);
frequency = pulseIn(sensorOut,LOW);
frequency = map(frequency, 25,72,255,0);
delay(100);
// ultrasonic function
int distance = readUltrasonicSensor();
if (distance <= 10 || redlight <= 255) {
leftFront.run()
}
}
Please don't post code as picture - post as text in a <CODE/> block
See How to get the best out of this forum for instructions; in particular, Posting code and common code problems
Please post your schematic, and some good, clear photos of your setup.
The key is not to try to do everything all at once.
Take small steps; do one thing at a time.
I try to but I just do not know the syntax yet
What does it mean? Does your code compiles? If not, please insert the error messages to the forum as text (the same way as the code).
I mean, I do not know how to code the function.
I want the ultrasonic sensor to read the distance
So take that as a step on its own.
There are lots of examples showing how to use those ultrasonic sensors ...
Would you like to make an example code for me?
As about your code, this lines not looks good for me...
Where the redlight variable defined?
Thank you
#include <AFMotor.h>
#include <tcs3200.h>
#define S0 12
#define S1 13
#define S2 9
#define S3 10
#define sensorOut 11
AF_DCMotor leftFront(1);
AF_DCMotor leftBack(2);
AF_DCMotor rightBack(3);
AF_DCMotor rightFront(4);
int trigPin1 = 6;
int echoPin1 = 7;
long duration, cm, inches;
int frequency = 0;
void setup() {
Serial.begin(9600);
// color sensor
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
// ultrasonic sensor
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void loop() {
// redlight function
digitalWrite(S2, LOW);
digitalWrite(S3,LOW);
frequency = pulseIn(sensorOut,LOW);
frequency = map(frequency, 25,72,255,0);
delay(100);
if (frequency <= 255){
leftFront.run(RELEASE);
leftBack.run(RELEASE);
rightFront.run(RELEASE);
rightBack.run(RELEASE);
} else {
leftFront.setSpeed(255);
leftFront.run(FORWARD);
leftBack.setSpeed(255);
leftBack.run(FORWARD);
rightFront.setSpeed(255);
rightFront.run(FORWARD);
rightBack.setSpeed(255);
rightBack.run(FORWARD);
}
// ultrasonic function
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
pinMode(echoPin1, INPUT);
duration = pulseIn(echoPin1, HIGH);
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
if (duration = 5){
leftFront.run(RELEASE);
leftBack.run(RELEASE);
rightFront.run(RELEASE);
rightBack.run(RELEASE);
} else {
leftFront.setSpeed(255);
leftFront.run(FORWARD);
leftBack.setSpeed(255);
leftBack.run(FORWARD);
rightFront.setSpeed(255);
rightFront.run(FORWARD);
rightBack.setSpeed(255);
rightBack.run(FORWARD);
}
}
Can you guys check this for me? I have upload the code to the car but it not running.
Would you like to check my new code?


