View unanswered posts | View active topics It is currently Sun May 26, 2019 7:03 pm

Reply to topic  [ 2 posts ] 
Writing a light sensor program 
Author Message

Joined: Fri Feb 18, 2011 12:25 am
Posts: 1
Post Writing a light sensor program
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S2, light2, sensorLightActive)
#pragma config(Sensor, S3, light3, sensorLightActive)
#pragma config(Sensor, S4, touch, sensorTouch)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop, encoder)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, touchServo, tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.

// Place code here to sinitialize servos to starting positions.
// Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.

/* 2 is right, 3 is left. Light sensors
#2: 25%
#3: 17%

#2: 41%
#3: 34%


task main()

while(SensorValue(touch) == 0) // While the Touch Sensor is inactive (hasn't been pressed):
while(SensorValue(light2) < 30)
motor[motorD] = 10;
motor[motorE] = 10;
while(SensorValue(light3) < 30)
motor[motorD] = 10;
motor[motorE] = 10;
that is what we have but how would we write a line following program with 2 different light sensors straddling the line please help

Fri Feb 18, 2011 12:28 am
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Writing a light sensor program
There are many ways to implement line following, from very simple to very sophisticated involving PID control. I will explain a simple way.

To write a line following algorithm, consider this:
- the robot is running forward at some speed
- the robot may turn slightly left or slightly right according to the light sensors (i.e. if the line is detected on the left, you turn left, if it is detected on the right, you turn right).
- assuming the white line will give a sensor value above some threshold number.
- if the line is right at the center between the two ligth sensors, both light sensors will give you values above the threshold.
- if only the right sensor gives you a value above threshold, the line is to your right.
- if only the left sensor gives you a value above threshold, the line is to your left.

With these considerations, the code would look something like this:
while (true)
    int leftSensor = SensorValue(leftLight);
    int rightSensor = SensorValue(rightLight);
    if ((leftSensor > threshold) && (rightSensor < threshold))
        // the line is to our left.
        motor[leftMotor] = 40;
        motor[rightMotor] = 60;
    else if ((leftSensor < threshold) && (rightSensor > threshold))
        // the line is to our right.
        motor[leftMotor] = 60;
        motor[rightMotor] = 40;
        // either we don't see the line or the line is right at center,
        // keep going forward straight.
        motor[leftMotor] = 50;
        motor[rightMotor] = 50;

Fri Feb 18, 2011 6:45 am
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.