Adding digital touch sensor TTP223B as start/stop button

Hello everyone, i'm trying to make an obstakel avoiding robot that's also a line tracker and want to use a touch sensor to start and stop the robot. but i can't seem to get it to work. anybody know how i would go about doing this? when i press the sensor it should start the loop named start. and when the touch sensor get's touched again is should go to void stop

edit: i got it to stop and start but it just keeps driving straight instead of checking if it follows the line.

anybody got a solution?

#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>

//hc-sr04 sensor
#define TRIGGER_PIN A2
#define ECHO_PIN A3
#define max_distance 50

//ir sensor
#define irLeft A0
#define irRight A1

//touch sensor
#define TouchSensor A4

//motor
#define MAX_SPEED 200
#define MAX_SPEED_OFFSET 20

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, max_distance);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);


int distance = 0;
int leftDistance;
int rightDistance;
boolean object;

unsigned long previousMillis = 0;     // Previous millis
unsigned long elapsedMillis = 0;      // Elapesed millis since touch
int ledState = LOW;                   // Current LED state
int debounceTime = 1000;              // Debounce time for the touch sensor

void setup() {
  Serial.begin(9600);
  pinMode(irLeft, INPUT);
  pinMode(irRight, INPUT);
  servo.attach(10);
  servo.write(90);

  motor1.setSpeed(120);
  motor2.setSpeed(120);


  pinMode(LED_BUILTIN, OUTPUT);
  

  digitalWrite(LED_BUILTIN, LOW);  // Turn builtin LED off
  
  
}

void loop()
{
  // Calculate the elapsed seconds
  elapsedMillis = millis() - previousMillis;

  // If the sensor is touched AND
  // the elapsed time is larger than the given debounce time
  if(digitalRead(TouchSensor) == 1 && elapsedMillis > debounceTime) {

    // Set ledState to LOW if it was HIGH and vice versa
    if(ledState == HIGH) {
      ledState = LOW;
      Stop();
    } else {
      ledState = HIGH;
      start();
    }

    // send ledState to the builtin LED
    digitalWrite(LED_BUILTIN, ledState);

    // Store the current millis on the previous millis
    previousMillis = millis();
  };

}
void start() {
  if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 0 ) {
    objectAvoid();
    //forword
  }
  else if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 1 ) {
    objectAvoid();
    Serial.println("TL");
    //leftturn
    moveLeft();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 0 ) {
    objectAvoid();
    Serial.println("TR");
    //rightturn
    moveRight();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 1 ) {
    //Stop
    Stop();
  }
}

void objectAvoid() {
  distance = getDistance();
  if (distance <= 15) {
    //stop
    Stop();
    Serial.println("Stop");

    lookLeft();
    lookRight();
    delay(100);
    if (rightDistance <= leftDistance) {
      //left
      object = true;
      turn();
      Serial.println("moveLeft");
    } else {
      //right
      object = false;
      turn();
      Serial.println("moveRight");
    }
    delay(100);
  }
  else {
    //forword
    Serial.println("moveforword");
    moveForward();
  }
}

int getDistance() {
  delay(50);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 100;
  }
  return cm;
}

int lookLeft () {
  //lock left
  servo.write(150);
  delay(500);
  leftDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print("Left:");
  Serial.print(leftDistance);
  return leftDistance;
  delay(100);
}

int lookRight() {
  //lock right
  servo.write(30);
  delay(500);
  rightDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print("   ");
  Serial.print("Right:");
  Serial.println(rightDistance);
  return rightDistance;
  delay(100);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);

}
void moveForward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);

}
void moveBackward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);

}
void turn() {
  if (object == false) {
    Serial.println("turn Right");
    moveLeft();
    delay(700);
    moveForward();
    delay(800);
    moveRight();
    delay(900);
    if (digitalRead(irRight) == 1) {
      loop();
    } else {
      moveForward();
    }
  }
  else {
    Serial.println("turn left");
    moveRight();
    delay(700);
    moveForward();
    delay(800);
    moveLeft();
    delay(900);
    if (digitalRead(irLeft) == 1) {
      loop();
    } else {
      moveForward();
    }
  }
}
void moveRight() {
  motor1.run(BACKWARD);
  motor2.run(FORWARD);

}
void moveLeft() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  
}
  

Touch sensor detects only electrically conductive objects. Such as you. Is that what you were expecting?

You have not posted the code so we can examine it. See instructions for posting. You can modify your post to do so - use the "pencil" icon below it.

Fritzings are considered as toys. . INO files are disliked. Imagine helpers downloading crappy files multiple times per day...

Refine Your post according to the advice in "How to get the best out of this forum". Link: How to get the best out of this forum - Using Arduino / Introductory Tutorials - Arduino Forum

yes, this is what i was expecting, the code has been uploaded properly now

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