I'm trying to Modify and program my robot car to sense light and distance. then move forward once it senses light

C++ code. the robot car head should swivel to look for the light source. i dont know what to do know. thank you.
' ' '
mutiple lines
' ' '
#include <Servo.h>
int servo = 0;
int vall =0;
int incriment = 0;
int count = 0;
Servo servo_A5;
int PhotoResistor = A3;
int motor1(int m1, int m2)
{
digitalWrite(200,200);
}
void swivelBack ()
{
servo_A5.write(count);
count += incriment;
if (count == 180) {
incriment = -1;
}
if (count == 0) {
incriment = 1;
}
delay(10);
}
int val(){
vall = analogRead(PhotoResistor);
Serial.println(vall);
return(vall);
}
void moveForward(int duration,int power)
{
if(count <=180)
{
analogWrite(11, power);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
analogWrite(10, power);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else (count >=0);
{
analogWrite(11, 0);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
analogWrite(10, 0);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}

	void setup()
	{
      	Serial.begin(9600);
		servo_A5.attach(A5, 500, 2500);
		pinMode (11,OUTPUT);
		pinMode(7,OUTPUT);
		pinMode(6,OUTPUT);
		pinMode(3,OUTPUT);
		pinMode(5,OUTPUT);
		pinMode(6,OUTPUT);
	}
	void loop()
	{
		moveForward(5000,255);
        swivelBack ();
        delay(100);
	}

First read the pinned post re 'How to get the most from the forum'

Your code keeps moving to the next line (from move forward to swivel, then back to the start) unless you make the code wait for each function (move forward and swivel). Right now, the code is not waiting.

I see you tried, but would you format your code again, and paste it between sets of three backtick (not single-quotes). ``` << those things... for a result that should look like this...

#include <Servo.h>
int servo = 0;
int vall = 0;
int incriment = 0;
int count = 0;
Servo servo_A5;
int PhotoResistor = A3;

int motor1(int m1, int m2)
{
  digitalWrite(200, 200);
}

void swivelBack ()
{
  servo_A5.write(count);
  count += incriment;
  if (count == 180) {
    incriment = -1;
  }
  if (count == 0) {
    incriment = 1;
  }
  delay(10);
}

int val() {
  vall = analogRead(PhotoResistor);
  Serial.println(vall);
  return (vall);
}

void moveForward(int duration, int power)
{
  if (count <= 180)
  {
    analogWrite(11, power);
    digitalWrite(7, HIGH);
    digitalWrite(6, LOW);
    analogWrite(10, power);
    digitalWrite(5, LOW);
    digitalWrite(4, HIGH);
  }
  else (count >= 0);
  {
    analogWrite(11, 0);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    analogWrite(10, 0);
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
  }
}

void setup()
{
  Serial.begin(9600);
  servo_A5.attach(A5, 500, 2500);
  pinMode (11, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  moveForward(5000, 255);
  swivelBack ();
  delay(100);
}

I also see you probably wanted one of the "6" (PWM) pins to be "10" (a DIO pin)

  • don't see code that uses the photo sensor value
  • don't see the purpose of moveForward() based on count which looks like the swivel angle

so

  • should the car only be made to move if a sufficiently high light sensor is detected and stopped when there is none

  • should the left and right motors be driven at different levels based on the swivel angle , only being equal when that angle is 90 deg directly in front

look this over (compiles but un-tested)
attempts to

  • capture sensor peak and angle of peak
  • slow motor on side of peak to turn toward peak
  • stop motors if peak sensor < some minimum sensor levels

test code is enabled to check pin assignments to move forward and INA and INB Forward/Reverse settings for left & right wheel

#include <Servo.h>

Servo servo_A5;

const byte PinPhotoResistor = A3;

const byte PinInALeft  = 4;
const byte PinInBLeft  = 5;
const byte PinEnLeft   = 10;

const byte PinInARight = 6;
const byte PinInBRight = 7;
const byte PinEnRight  = 11;

int ang       = 0;
int dir       = 1;

const int MinSensorVal  =  50;
int sensorValLst;
int sensorValLst2;
int sensorVal;
int sensorValPeak;
int sensorPeakAng;

char s [90];

// -----------------------------------------------------------------------------
void
swivelSensor ()
{
    // advance ang and reverse dir at endpoints
    ang += dir;
    if (ang <= 0 && 180 <= ang)
        dir = -dir;

    // move sensor servo
    servo_A5.write (ang);

    // read sensor
    sensorVal = analogRead (PinPhotoResistor);

    // capture peak
    if (sensorValLst2 <= sensorValLst && sensorValLst > sensorVal)  {
        sensorValPeak = sensorVal;
        sensorPeakAng = ang;
    }

    // update previous values (no quite right at endpoints)
    sensorValLst2 = sensorValLst;
    sensorValLst  = sensorVal;
}

// -----------------------------------------------------------------------------
enum { For, Rev };

void setMotorPwm (
    int  pwm,
    int  dir,
    byte pinEn,
    byte pinInA,
    byte pinInB )
{
    sprintf (s, "    setMotorPwm: en-pin %d, pwm %3d", pinEn, pwm);
    Serial.println (s);

    if (For == dir)  {
        digitalWrite (pinInA, HIGH);
        digitalWrite (pinInB, LOW);
    }
    else {
        digitalWrite (pinInA, LOW);
        digitalWrite (pinInB, HIGH);
    }

    analogWrite  (pinEn,  pwm);
}

// -----------------------------------------------------------------------------
const int PWM  = 100;           // default pwm value

void motorAdjust ()
{
    sprintf (s, "moveAdjust: sensor %6d, ang %3d",
                                sensorValPeak, sensorPeakAng);
    Serial.println (s);

    int pwmLeft  = PWM;
    int pwmRight = PWM;

    if (MinSensorVal > sensorValPeak)   // stop
        pwmLeft = pwmRight = 0;
    else if (sensorPeakAng < 85)        // to left
        pwmLeft  /= 2;                  // slow left motor
    else if (95 < sensorPeakAng)        // to right
        pwmRight /= 2;                  // slow right motor

    setMotorPwm (pwmLeft,  For, PinEnLeft,  PinInALeft,  PinInBLeft);
    setMotorPwm (pwmRight, For, PinEnRight, PinInARight, PinInBRight);
}

// -----------------------------------------------------------------------------
void loop ()
{
    // test code, turn each motor on/off for 2 sec
    if (true)  {
        setMotorPwm (PWM, For, PinEnLeft,  PinInALeft,  PinInBLeft);
        delay (2000);
        setMotorPwm (  0, For, PinEnLeft,  PinInALeft,  PinInBLeft);

        setMotorPwm (PWM, For, PinEnRight, PinInARight, PinInBRight);
        delay (2000);
        setMotorPwm (  0, For, PinEnRight, PinInARight, PinInBRight);
    }

    else {
        swivelSensor ();
        motorAdjust  ();
        delay        (100);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    servo_A5.attach (A5, 500, 2500);

    pinMode (PinInARight, OUTPUT);
    pinMode (PinInARight, OUTPUT);
    pinMode (PinEnRight,  OUTPUT);

    pinMode (PinInALeft,  OUTPUT);
    pinMode (PinInALeft,  OUTPUT);
    pinMode (PinEnLeft,   OUTPUT);

    setMotorPwm (0, For, PinEnLeft,  PinInALeft,  PinInBLeft);
    setMotorPwm (0, For, PinEnRight, PinInARight, PinInBRight);
}

Can someone please add code so my super sonic sensor can see distance so that is stops

C++

#include <Servo.h>
int servo = 0;
int vall = 0;
int incriment = 0;
int count = 0;
Servo servo_A5;
int PhotoResistor = A3;

int motor1(int m1, int m2)
{
digitalWrite(200, 200);
}

void swivelBack ()
{
servo_A5.write(count);
count += incriment;
if (count == 180) {
incriment = -5;
}
if (count == 0) {
incriment = 5;
}
delay(10);
}

int val() {
vall = analogRead(PhotoResistor);
Serial.println(vall);
return (vall);
}
int duration = 5000;
int power = 255;
void moveForward(int duration, int power)
{
if (count <= 180)
{
analogWrite(11, power);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
analogWrite(3, power);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else (count >= 0);
{
analogWrite(11, 0);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
analogWrite(3, 0);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}

void setup()
{
Serial.begin(9600);
servo_A5.attach(A5, 500, 2500);
pinMode(11, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{
moveForward(5000, 255);
swivelBack ();
delay(100);
}

First, please repost your code using code tags.

I don't see a distance sensor, only a photoresistor.
Please explain how your design works.

I have merged your cross-posts @henrytroyal.

Instead of wasting everyone's time by creating a new topic, you should read and respond to the suggestions provided here in your original topic!

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

@henrytroyal - Please, put your program code in a code block.

Write pseudocode to capture the behavior of your robot. Your robot must perform the following actions:

Scan left and right to identify the brightest light source
Drive towards it while monitoring the ultrasonic distance sensor
Periodically check to make sure it’s still traveling the right direction
Stop when the target is ~12" (30 cm) away

@henrytroyal - What you have IS pseudo code.

1 Like

Are you permitted to use an AI chatbot for this assignment.?
I had the following dialogue with deepseek and got quite a plausible looking answer. However, I am not repeating the actual pseudo code it generated:

Student: I have received the following assignment in my arduino robotics class. Can you help?

Deepseek: Of course! I'd be happy to help with your Arduino robotics assignment. Please share the details of the assignment, and I'll do my best to assist you. What specific task or project are you working on?

Student: Write pseudocode to capture the behavior of your robot. Your robot must perform the following actions:

Scan left and right to identify the brightest light source
Drive towards it while monitoring the ultrasonic distance sensor
Periodically check to make sure it’s still traveling the right direction
Stop when the target is ~12" (30 cm) away

Deepseek:
. . .
. . .

I have merged your topics due to them having too much overlap on the same subject matter @henrytroyal.

In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.

The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Instead of wasting time creating redundant forum topics, it will be much more productive for you to instead act on the responses the forum helpers have already made on your existing topic.

Thanks in advance for your cooperation.

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