Author Topic: $50 Robot, Adding 3 more photo sensors, need help with programming  (Read 609 times)

0 Members and 1 Guest are viewing this topic.

Offline zwarteTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
As the title said, i have build-ed the $50 robot, and i would like to add 3 more photo sensors, for some reason 4 sensors wont work and it's not the connection. using led as indicator of input/controle  change.

My edited code:
Code: [Select]
/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_on();//turn LED on


while(1)
{
//store sensor data
sensor_front_left=a2dConvert8bit(5);
sensor_front_right=a2dConvert8bit(4);
sensor_right=a2dConvert8bit(3);
sensor_left=a2dConvert8bit(2);
sensor_ass=a2dConvert8bit(1);


        //detects light infront of robot
        if(sensor_front_left = sensor_front_right && (sensor_front_left = sensor_front_right) > threshold)
   { //If front sensors are equal on light, go straight
   LED_off();
   servo_left(25);
   servo_right(44);
   LED_on();
   }

//detects more light on left side of robot
else if(sensor_left > sensor_front_left && (sensor_left - sensor_front_left) > threshold)
{//go left
LED_off();
servo_left(44);
servo_right(44);
LED_on();
}

//detects more light on right side of robot
else if(sensor_right > sensor_front_right && (sensor_right - sensor_front_right) > threshold)
{//go right
LED_off();
servo_left(25);
servo_right(25);
LED_on();
}

//detects light behind robot
else if(sensor_ass > sensor_front_left &&  sensor_ass > sensor_front_right && sensor_ass > sensor_right && sensor_ass > sensor_left > threshold)
{//go back
LED_off();
servo_left(44);
servo_right(25);
LED_on();
}

The back (ass) sensor is tenth for driving back if it accidently drove in a dark space.
ANy programming ideas?

Offline jkerns

  • Robot Overlord
  • ****
  • Posts: 244
  • Helpful? 12
Re: $50 Robot, Adding 3 more photo sensors, need help with programming
« Reply #1 on: November 07, 2012, 03:31:03 PM »
What do you mean by "won't work"? You are not getting the readings into the MCU? The motors are not changing speeds? It's not following the line well? Or...???
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

Offline zwarteTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
Re: $50 Robot, Adding 3 more photo sensors, need help with programming
« Reply #2 on: November 08, 2012, 09:08:42 AM »
i managed to get it working with 5 sensors.

Code: [Select]
int main(void)
{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_front_left=0;//left photoresistor
int sensor_front_right=0;//right photoresistor
int sensor_right=0;//left photoresistor
int sensor_left=0;//right photoresistor
int sensor_behind=0;//right photoresistor
int threshold=6;//the larger this number, the more likely your robot will drive straight


/****************INITIALIZATIONS*******************/
//other stuff Im experimenting with for SoR
//uartInit();  // initialize the UART (serial port)
//uartSetBaudRate(9600);// set the baud rate of the UART for our debug/reporting output
//rprintfInit(uartSendByte);// initialize rprintf system

//timerInit(); // initialize the timer system

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

//rprintf("Initialization Complete\r\n");
/**************************************************/


/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_off();//turn LED on


while(1)
{
//store sensor data
sensor_front_left=a2dConvert8bit(5);
sensor_front_right=a2dConvert8bit(4);
        sensor_right=a2dConvert8bit(3);
sensor_left=a2dConvert8bit(2);
sensor_behind=a2dConvert8bit(1);

//detects more light on left side of robot
if(sensor_front_left > sensor_front_right && (sensor_front_left - sensor_front_right) > threshold)
{//go left
servo_left(44);
servo_right(44);
}

//detects more light on right side of robot
else if(sensor_front_right > sensor_front_left && (sensor_front_right - sensor_front_left) > threshold)
{//go right
servo_left(25);
servo_right(25);
}

//detects more light on left side of robot
else if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
{//go left
servo_left(44);
servo_right(44);
}

//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
{//go right
servo_left(25);
servo_right(25);
}

        else if(sensor_behind > sensor_front_left + sensor_front_right && (sensor_behind - sensor_front_right - sensor_front_left) > threshold)
            {
             servo_left(44);
servo_right(25);
}

//light is about equal on both sides
else
{//go straight
servo_left(25);
servo_right(44);
}

 


Get Your Ad Here