hello i am building a boat that fishes trash out of the water. now we have 2 codes that i smashed together in one big code but now my propulsion system doesnt work. let me explain the layout.
the boat has 2 waterwheels on each side of the boat. the RPM is controlled by a sonar sensor. so when it gets to close to the side it will correct one side of the waterwheel. that whas one code,
the second code is for a system to sort different color plastic balls,
both systems worked fine with two codes. but when i smashed them together the boats propulsion system doesnt work anymore. i used serial print to see if the sonar sensor gets the right readings and it does. am a bit lost help would be much appreciated.
// motor pins
int motorR1 = 2;
int motorR2 = 3;
int motorL1 = 4;
int motorL2 = 5;
int pmwR = 6;
int pmwL = 9;
// sonar variable
long duration1; //zijkant
int distance1;
long duration2; //voorkant
int distance2;
//sonar pins
const int trigPinF = 7; //voorkant
const int echoPinF = 8;
// Define color sensor pins
#include <Servo.h>
Servo ServoR; //rood
Servo ServoG; //groen
Servo ServoB; //blauw
Servo ServoY; //geel
#define S0 10
#define S1 11
#define S2 12
#define S3 13
#define sensorOut A4
// Variables for Color Pulse Width Measurements
int redPW = 0;
int greenPW = 0;
int bluePW = 0;
void setup() {
{ // servo pins
ServoR.attach(A0); //rood
ServoG.attach(A1); //groen
ServoB.attach(A2); //blauw
ServoY.attach(A3); //geel
// Set S0 - S3 as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
}
{ // Set Pulse Width scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
}
{ // motor
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
// pwm motors
pinMode(6, OUTPUT); //Rechts
pinMode(9, OUTPUT); //Links
}
{ // sonar sensors F
pinMode(trigPinF, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinF, INPUT); // Sets the echoPin as an Input
}
{ // Setup Serial Monitor
Serial.begin(9600);
}
}
void loop() {
{ // aandrijving en sonar sensor
if (distance2 <30 && distance2 > 40)
{
analogWrite(6, 180);
analogWrite(9, 180);
}
if (distance2 < 30)
{
analogWrite(6, 255);
analogWrite(9, 0);
}
if (distance2 > 40)
{
analogWrite(6, 0);
analogWrite(9, 180);
}
// motors
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
}
{
//sonar sensor F
digitalWrite(trigPinF, LOW); // sonar sensor berekening naar cm zijkant
delayMicroseconds(2);
digitalWrite(trigPinF, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinF, LOW);
duration2 = pulseIn(echoPinF, HIGH);
distance2 = duration2 * 0.034 / 2;
}
{ // 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
delay(200);
// 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);
//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);
delay(10000);
}
if (greenPW < 100 && redPW > 100 && bluePW > 90) {
ServoG.write(30);
delay(10000);
}
if (bluePW < 97 && redPW > 100 && greenPW > 90) {
ServoB.write(30);
delay(10000);
}
if (redPW < 80 && greenPW < 80 && bluePW <105) {
ServoY.write(60);
delay(10000);
}
}
}
}
// Function to read Red Pulse Widths
int getRedPW() {
digitalWrite(S2,LOW); // Set sensor to read Red only
digitalWrite(S3,LOW);
int PW; // Define integer to represent Pulse Width
PW = pulseIn(sensorOut, LOW); // Read the output Pulse Width
return PW; // Return the value
}
// Function to read Green Pulse Widths
int getGreenPW() {
digitalWrite(S2,HIGH); // Set sensor to read Green only
digitalWrite(S3,HIGH);
int PW; // Define integer to represent Pulse Width
PW = pulseIn(sensorOut, LOW); // Read the output Pulse Width
return PW; // Return the value
}
// Function to read Blue Pulse Widths
int getBluePW() {
digitalWrite(S2,LOW); // Set sensor to read Blue only
digitalWrite(S3,HIGH);
int PW; // Define integer to represent Pulse Width
PW = pulseIn(sensorOut, LOW); // Read the output Pulse Width
return PW; // Return the value
}