Capacitive Sensor Project

I have a robotics project wherein when the capacitive proximity sensor detects and lights up its on-sensor LED, the robot car stops and rotates 4 servo motors. The capacitive proximity sensor is onboard an obstacle avoidance car that uses an ultrasonic sensor.

I have given it a try to create the project during which the capacitive sensor didn't need any code and when it detects and lights up it automatically stops the wheels which is what I need but don't know why.
(Here's the code:)

int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04

long duration, distance;

void setup() {
  
  delay(random(500,2000));   // delay for random time
  Serial.begin(9600);
  pinMode(4, OUTPUT);      // set Motor pins as output
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves

}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
  delay(10);
  
    // If you dont get proper movements of your robot then alter the pin numbers
  if (distance > 16){
    digitalWrite(4, LOW);  // move forward
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);                                
    digitalWrite(7, HIGH);                                                       
  }

  if (distance < 15){
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                              
    digitalWrite(7, LOW);
    delay(1000);
    digitalWrite(4, HIGH); //movebackword         
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);                                
    digitalWrite(7, LOW);
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(1000);  
    digitalWrite(4, LOW);  //move the other direction     
    digitalWrite(5, HIGH);   
    digitalWrite(6, LOW);                                 
    digitalWrite(7, LOW);  
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);  
    delay(1000); 
  }
}

But when I add the capacitive proximity sensor and servo motors to the code, the added code doesn't seem to do anything.
(Here's the code:)

#include <Servo.h>

Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;

#define servoPin0 36
#define servoPin1 37
#define servoPin2 38
#define servoPin3 39

int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04

long duration, distance;

int capacitive = 32;
int val = 0;


void setup() {
  
  delay(random(500,2000));   // delay for random time
  Serial.begin(9600);
  pinMode(4, OUTPUT);      // set Motor pins as output
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves

  pinMode(capacitive, INPUT);

  myservo0.attach(servoPin0);
  myservo1.attach(servoPin1);
  myservo2.attach(servoPin2);
  myservo3.attach(servoPin3);
  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
  delay(10);
  
    // If you dont get proper movements of your robot then alter the pin numbers
  if (distance > 16){
    digitalWrite(4, LOW);  // move forward
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);                                
    digitalWrite(7, HIGH);                                                       
  }

  if (distance < 15){
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);
    delay(1000);
    digitalWrite(4, HIGH); //movebackword         
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);                                
    digitalWrite(7, LOW);
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);  
    delay(1000);  
    digitalWrite(4, LOW);  //move the other direction     
    digitalWrite(5, HIGH);   
    digitalWrite(6, LOW);                                 
    digitalWrite(7, HIGH);  
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);  
    delay(1000); 
  }

  val = digitalRead(capacitive);
  if (val == HIGH) {
    myservo0.write(90);
    delay(500);
    myservo1.write(90);
    delay(500);
    myservo2.write(90);
    delay(500);
    myservo3.write(90);
    delay(1000);
    myservo0.write(180);
    delay(500);
    myservo1.write(180);
    delay(500);
    myservo2.write(180);
    delay(500);
    myservo3.write(180);
    delay(1000);
  }
}

I attached a photo of the connections I made.
P.S. Sorry for the confusing fritzing breadboard sketch, it's the best that I can currently do.
I don't know what is the problem why when the capacitive proximity sensor detects something and lights up, the servo still doesn't do anything. If it is a connection error or code error please correct me and if there is no problem with it, I'll try to re-check my project again.

Replace the 9V battery and connect the servo motors to another power source, the Arduino a Power Supply it is NOT. My guess is the 9V is collapsing on you.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.