That is the problem. Every line of a program has to be in the right place, perform the correct task, and not conflict with any other lines.
Go through your code line by line, and make sure that every one of them makes sense.
That is the problem. Every line of a program has to be in the right place, perform the correct task, and not conflict with any other lines.
Go through your code line by line, and make sure that every one of them makes sense.
OK so no worries with this type of breadboard as long as you connected the right columns
for the code I cleaned it up a bit and put the delay() back in there as you did not understand how to use millis for asynchronous tasks.
I typed that here in the forum based on your code, so may be there are some typos.
the pin names would benefit of consistently have the word pin in the name (eg you should use pinMotorR1 instead of motorR1....)
#include <Servo.h>
Servo ServoR; //rood
Servo ServoG; //groen
Servo ServoB; //blauw
Servo ServoY; //geel
const int ServoPinRood = A0; //rood
const int ServoPinGroen = A1; //groen
const int ServoPinBlauw = A2; //blauw
const int ServoPinGeel = A3; //geel
// color sensor
const int pinS0 = 10;
const int pinS1 = 11;
const int pinS2 = 12;
const int pinS3 = 13;
const int sensorOut = A4;
// motor pins
const int motorR1 = 2;
const int motorR2 = 3;
const int motorL1 = 4;
const int motorL2 = 5;
const int pmwR = 6;
const int pmwL = 9;
//sonar pins
const int trigPinF = 7;
const int echoPinF = 8;
// sonar variable
double distance2;
// Variables for Color Pulse Width Measurements
unsigned long redPW = 0;
unsigned long greenPW = 0;
unsigned long bluePW = 0;
// Function to read Red Pulse Widths
unsigned long getRedPW() {
digitalWrite(pinS2, LOW); // Set sensor to read Red only
digitalWrite(pinS3, LOW);
return pulseIn(sensorOut, LOW); // Return the output Pulse Width
}
// Function to read Green Pulse Widths
unsigned long getGreenPW() {
digitalWrite(pinS2, HIGH); // Set sensor to read Green only
digitalWrite(pinS3, HIGH);
return pulseIn(sensorOut, LOW); // Return the output Pulse Width
}
// Function to read Blue Pulse Widths
unsigned long getBluePW() {
digitalWrite(pinS2, LOW); // Set sensor to read Blue only
digitalWrite(pinS3, HIGH);
return pulseIn(sensorOut, LOW); // Return the output Pulse Width
}
double getDistance() {
digitalWrite(trigPinF, LOW); // sonar sensor berekening naar cm zijkant
delayMicroseconds(2);
digitalWrite(trigPinF, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinF, LOW);
return pulseIn(echoPinF, HIGH) * 0.034 / 2;
}
void setup() {
// servo pins
ServoR.attach(ServoPinRood); //rood
ServoG.attach(ServoPinGroen); //groen
ServoB.attach(ServoPinBlauw); //blauw
ServoY.attach(ServoPinGeel); //geel
// Set pinS0 - pinS3 as outputs
pinMode(sensorOut, INPUT);
pinMode(pinS0, OUTPUT);
pinMode(pinS1, OUTPUT);
pinMode(pinS2, OUTPUT);
pinMode(pinS3, OUTPUT);
// Set Pulse Width scaling to 20%
digitalWrite(pinS0, HIGH);
digitalWrite(pinS1, LOW);
// motor
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
// pwm motors
pinMode(pmwR, OUTPUT); //Rechts
pinMode(pmwL, OUTPUT); //Links
// sonar sensors F
pinMode(trigPinF, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinF, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
}
void loop() {
distance2 = getDistance();
// kleuren sensor en servo's
redPW = getRedPW(); // Read Red Pulse Width
delay(200);
greenPW = getGreenPW(); // Read Green Pulse Width
delay(200);
bluePW = getBluePW(); // Read Blue Pulse Width
// Print output to Serial Monitor
Serial.print("Red PW = "); Serial.print(redPW);
Serial.print(" - Green PW = "); Serial.print(greenPW);
Serial.print(" - Blue PW = "); Serial.println(bluePW);
// motors aandrijving
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
if (distance2 < 30) { // distance is less than 30
analogWrite(pmwR, 255);
analogWrite(pmwL, 0);
}
else if (distance2 <= 40) { // distance is between 30 and 40
analogWrite(pmwR, 180);
analogWrite(pmwL, 180);
} else { // distance is more than 40
analogWrite(pmwR, 0);
analogWrite(pmwL, 180);
}
//servo move
if (redPW > 120 && greenPW > 120 && bluePW > 120) {
ServoR.write(0);
ServoG.write(85);
ServoB.write(85);
ServoY.write(0);
}
else if (redPW < 80 && greenPW > 80 && bluePW > 80) {
ServoR.write(60);
}
else if (greenPW < 100 && redPW > 100 && bluePW > 90) {
ServoG.write(30);
}
else if (bluePW < 97 && redPW > 100 && greenPW > 90) {
ServoB.write(30);
}
else if (redPW < 80 && greenPW < 80 && bluePW < 105) {
ServoY.write(60);
}
delay(1000);
}
note that pulseIn() returns an unsigned long so I changed the type of the variables such as redPW and the functions returning the values
For extra information and examples for millis() look at
you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction
hey thank you for the details and time. i can see the diffrences, i will try to learn them why you do things certain ways. i guess my logic procces.
i do have some questions. the calculation for the sonar sensor is set in a function? and when that, getDistance variabal is needed it will execute the sonar sensor? do i understand this correct?
second thing after looking the if statemetns up with else i can see why you use the else if.
i will let you know if it works thanks a lot to show me. i can now decode it and learn from it.
yes, to make the main code more readable. It's the same as you have done with getGreenPW() for example, you use a function.
By the way, note that I changed also the Serial baud rate to 115200. No need to go slow
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.