Thread: URGENT - Atmega328p - Robot Sumo code not working

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    1

    URGENT - Atmega328p - Robot Sumo code not working

    Hi, guys! I have been working on my Robot Sumo project for months and it still doesn't work and i can't tell why is that. My project is due tomorrow so i'm in need of urgent help.
    I use an Atmega328p, programmed with Atmel Studio 7.0, as a processing unit and two tcr5000 line sensors, one on the front of the robot and one on the back. To control the motors I use an L293D. When I test the code the robot even moves, but when the tcr5000 sensors detect the black colour it does not invert the direction of march as it was supposed to. I have already checked and the voltage values that come out of the sensors are correct, voltages very close to 5V for the white colour and reduced voltages for the black colour, so the problem must be with the code.

    The code I am using is the following:


    Code:
    #define F_CPU 16000000
    #include <stdint.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>
    
    
    uint8_t flagdadosrecebidos = 0;
    uint8_t flagligado = 0;
    uint8_t flagfe = 0;
    uint8_t flagfd = 0;
    uint8_t flagte = 0;
    uint8_t flagtd = 0;
    uint8_t flagatuarmotores = 0;
    uint8_t i;
    uint8_t ligado = 0;
    int fe = 0;
    int fd = 0;
    int te = 0;
    int td = 0;
    int feito = 0;
    
    
    unsigned int MaxSVal = 716;
    
    
    
    
    void init(void)
    {
    	ADMUX  |= (1 << REFS0);
    	ADCSRA |= (1 << ADEN)|(1 << ADPS2)|(1 << ADPS1)|(1<<ADPS0);
    	
    	DDRD |= (1<<PORTD3)|(1<<PORTD5)|(1<<PORTD6)|(1<<PORTD7);
    	DDRB |= (1<<PORTB0)|(1<<PORTB3)|(1<<PORTB4)|(1<<PORTB5);
    	TCCR2A |=(1 << COM2A1) | (1 << COM2B1) | (1 << WGM21) | (1 << WGM20);
    	TCCR2B |= (1<<CS20);
    	sei();
    }
    
    
    void ler_sl(void)
    {
    	ADMUX = 0B01000000;
    	switch (ADMUX){ // vamos ler os valores recebidos nos pins analogicos para ver se estamos no branco ou no preto
    		
    		case(0B01000000): // ler o primeiro sensor que se vai encontrar em frente na esquerda
    		ADCSRA |= (1 << ADSC);
    		while ((ADCSRA & (1<<ADSC)) !=0)
    		{
    			if (ADCH < MaxSVal) // se estiver a ver preto ADCH > 175 a flag do sensor ativa e posteriormente alterara o comportamento das rodas
    			{
    				fe = 1;
    				}else{
    				fe = 0;
    			}
    		}
    		ADMUX = 0B01000010;
    		
    		case(0B01000010): // ler o terceito sensor, traseira esquerda
    		ADCSRA |= (1 << ADSC);
    		while ((ADCSRA & (1<<ADSC)) !=0)
    		{
    			if (ADCH < MaxSVal)
    			{
    				td = 1;
    				}else{
    				td = 0;
    			}
    		}
    		ADMUX = 0B01000000;
    	}
    
    
    }
    
    
    void atuarmotores(void){
    	if ((fe == 1))
    	{
    		PORTD = (1<<PORTD7);
    		PORTB = (1<<PORTB4);
    		OCR2A = 255;
    		OCR2B = 255;
    	}
    	if ((td == 1))
    	{
    		PORTD = (1<<PORTD5);
    		PORTB = (1<<PORTB5);
    		OCR2A = 255;
    		OCR2B = 255;
    	}
    }
    
    
    int main(void)
    {
    	init();
    	OCR2A = 255;
    	OCR2B = 255;
    	PORTD = (1<<PORTD5);
    	PORTB = (1<<PORTB5);
    	while (1)
    	{
    
    
    		ler_sl();
    		atuarmotores();
    	}
    }
    I appreciate all the help possible, it is a very important project.
    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have been working on my Robot Sumo project for months and it still doesn't work and i can't tell why is that. My project is due tomorrow
    And you couldn't have like turned up a week ago, where there would be a reasonable amount of time to help you.

    Nope, you're down by 7 and you're 90 yards from the end zone with 5 seconds left on the clock.
    Hoping beyond all hope that one miracle play will save you.


    1. This can't possibly be all your code. Nearly all your variables are unused.
    It looks like you ripped a bunch of stuff out and posted the remains.

    2. The switch statement in ler_sl is useless, and line 47 makes it useless.
    It's further crippled by the lack of break statements.

    3. atuarmotores only looks at two of your four variables. It seems only capable of turning something on, not turning it off again.

    > two tcr5000 line sensors, one on the front of the robot and one on the back.
    4. Use some meaningful constants.
    There's no way for us to meaningfully guess which bits of which register correspond to which sensor or motor.

    Start by defining meaningful names for each bit of hardware you interact with.
    Code:
    #define TCR_FRONT  (1 << ADPS2)
    #define TCR_BACK   (1 << ADPS1)
    Your functional code should read like
    Code:
    if ( (readsensor() & TCR_FRONT == TCR_FRONT) ) {
      // stop forward motion
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help reading pseudo code for car robot
    By connectrme in forum C Programming
    Replies: 5
    Last Post: 09-12-2014, 07:48 PM
  2. robot code
    By pureflip428 in forum C Programming
    Replies: 2
    Last Post: 12-03-2012, 08:28 PM
  3. Urgent help please with C code
    By Clement Museka in forum C Programming
    Replies: 5
    Last Post: 06-15-2012, 03:50 AM
  4. could you help with me C code?thanks,a little urgent.
    By hth373737 in forum C Programming
    Replies: 50
    Last Post: 11-24-2008, 07:12 AM
  5. Urgent help in code...
    By alvifarooq in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2004, 07:40 AM

Tags for this Thread