Using Signal from ADC

Hi
I have an OX2 and I am using Sharp IR Detectors to determine object distances. I have been able to use the ADC to convert the IR signal to digital which I am displaying as a HEX number (Ex. 8E for a certain distance). Now I want to use this number to tell my robot to either keep forward, back, turn etc. which are functions that I have predfined prior to main. I am not sure how to move on from here. I am using A7 as the analog input to the ADC. Please help.
Thanks

I just noticed that I should not use A6 and A7 as inputs. But the question is still the same for A0-A5.
Thanks

Hello.

Your question seems to be a bit vague, so I’m not exactly sure how to answer it. In short, your program should consist of a main loop that executes over and over again. You would accomplish this via something like:

int main()
{
  // initialization code goes here

  while (1) // main loop
  {
    // your program code goes here
    loop();
  }
  return 0;
}

In your main loop you’ll call the function you have for reading the sharp distance sensor. You can store the value in a variable and act based on the reading:

void loop()
{
  int reading = readDistanceSensor();

  if (reading < MIN_DISTANCE)
    moveBackwards();
  else if (reading < SOME_OTHER_DISTANCE)
    doSomethingElse();
  else
    ...
}

- Ben

Thanks Ben for the help. I know about using tha main loop. The question is how do I use the ADC converted number (say 7E) as a code “conditioner” i.e. to make decisions. Here is the code I am using so far. Where and how do I use this information to make the robot behave as I want? I guess the problem is that I am expecting to use the ADC output in a pin signal but it may be because my C programing skills are poor and don’t understand how to assign the reading to a variable and then use the variable to set the condition.
thanks

Here is my code:

#include <avr/io.h>
#include "LCD.h"
#include "SPI.h"


void stop()
{
        brakeHighMotor2( 0 );
        brakeHighMotor1( 0 );
}

void turnright()

{
        //delay_ms(250);
        //stop();
        setMotor1 (150);
        setMotor2 (-150);
        delay_ms(700);
        stop();
    //    delay_ms(700);
}

void turnrighthome()

{
        delay_ms(250);
        stop();
        setMotor1 (84);
        setMotor2 (-80);
        delay_ms(740);
        stop();
    //    delay_ms(700);
}

void turnrightfast()

{
        delay_ms(220);
        stop();
        setMotor1 (150);
        setMotor2 (-146);
        delay_ms(450);  //480 normal //was 470
        stop();
}

void turnrightsharp()

{
        stop();
        setMotor1 (84);
        setMotor2 (-80);
        delay_ms(750);
        stop();
    //    delay_ms(700);
}
void turnleft()
{
        //delay_ms(515);
        //stop();
        setMotor1 (130);
        setMotor2 (-120);
        delay_ms(750);
        stop();
}

void straight()
{
        setMotor1 (150);
        setMotor2 (150);
}


void reverse()
{
        setMotor1 (-84);
        setMotor2 (-81);
}
void uturn()
{
        delay_ms(550);
        stop();
        setMotor1 (84);
        setMotor2 (-80);
        delay_ms(1400); //was 1600 //was 1420
        stop();
        delay_ms(600);
}


//IR Sensor test
void adcstart(void)
{
	LCDString( "distance = trimpot 0x" );	

	ADMUX = 0x27;			// bit 7 and 6 clear: voltage ref is Vref pin
							// bit 5 set: left-adjust result (only need ADCH)
							// bit 4 not implemented
							// bits 0-3: ADC channel (channel 7)
while (1)
	
	{
		ADCSRA |= ( 1 << ADSC );			// start conversion
		while ( ADCSRA & ( 1 << ADSC ));	// wait for conversion to finish
		unsigned char distance = ADCH;
		LCDHex( distance );
		LCDMoveCursor( LCD_ROW_0 + 30 );// move cursor to top row, column 18
										//  (just after "speed = trimpot 0x")
}		
		
}




int main()

{
	SPIInit();			// allows the mega644 to send commands to the mega168
	LCDInit();	
	

	// initialize ADC
	ADCSRA = 0x87;		// bit 7 set: ADC enabled
						// bit 6 clear: don't start conversion
						// bit 5 clear: disable autotrigger
						// bit 4: ADC interrupt flag
						// bit 3 clear: disable ADC interrupt
	
	
						// bits 0-2 set: ADC clock prescaler is 128

	
	adcstart();	

	return 0;
}

Hi
Can someone please help me with this question?
Thanks

You already have the sensor reading stored in a variable called “distance” in your adcstart() function:

unsigned char distance = ADCH;

You’re already taking the action of displaying the reading on the LCD. In place of that (or in addition to that), you can take motor actions based on the value of that variable. For example:

while (1)
{
  ADCSRA |= ( 1 << ADSC ); // start conversion
  while ( ADCSRA & ( 1 << ADSC )); // wait for conversion to finish
  unsigned char distance = ADCH;
  LCDHex( distance );
  LCDMoveCursor( LCD_ROW_0 + 30 );// move cursor to top row, column 18
  // (just after "speed = trimpot 0x")

  if (distance < MIN_DISTANCE)
    moveBackwards();
  else if (distance < SOME_OTHER_DISTANCE)
    doSomethingElse();
  else
    ...
}

- Ben

Thanks
I was able to make it work with your help