-4
\$\begingroup\$

i'm working on a school project "Android control a robot car via Bluetooth" with temperature sensor "LM35" , gas detector "MQ-2" ,motor drive IC "l293D" and Bluetooth module "HC-05" using atmega 32 . i'm already wrote and run my code and there is no error in the code appeared to me, so i moved on to make the connection as hardware but nothing of my robot work. i searched a lot to find out where's my problem in the code or the connection,but i didn't get the Convincing answer . Hint: i tried every component individual to check if it's work or not ,it works. but when i burn the code which attached below nothing is work .. can any one help me to find out what's my problem ? here is the code

main.c

#include "type.h"
#include "avr/io.h"
#define F_CPU 16000000
#include "avr/delay.h"
#include"UART_interface.h"
#include"TempCheck.h"
#include"SmokeCheck.h"
void main(void)
{
    DDRB=0XFF;
    DDRA = 0b00000000;
    DDRC = 0b00000001;

    while(1)
    {
        motor();
        Tempcheck();
        Smokecheck ();
    }
}

directions.c

#include "type.h"
#include "avr/io.h"
#define F_CPU 16000000
#include "avr/delay.h"
#include"UART_interface.h"
void motor (void)
{
    while(1)
    {
        u32 x;
        x=UART_u8RecievedByte();
                switch(x)
                {
                case('F'):
                        {
                    PORTB=0b00000101; // car move in forward direction
                    _delay_ms(500);
                    break;
                        }
                case('B'):
                    {
                    PORTB=0b00001010;  // car move in backward direction
                    _delay_ms(500);
                    break;
                    }
                case('R'):
                    {
                    PORTB=0b00000001;   // car move in right direction
                    _delay_ms(500);
                    break;
                    }
                case('L'):
                    {
                    PORTB=0b00000100;    // car move in left direction
                    _delay_ms(500);
                    break;

                    }
                }
    }
}

TempCheck.c

#include "type.h"
#include "avr/io.h"
#define F_CPU 16000000
#include "avr/delay.h"
#include "temp.h"
void Tempcheck (void)
{
    u32 y;
    temp_init();
    while(1)
    {
        y=temp_read();
        y = (y * 5000)/ 256;
        y = y/10;
        if (y>60)
        {
            PORTC = 0b00000001;  //turn on the buzzer
            _delay_ms(500);
            PORTC = 0b00000000;  //turn off the buzzer
            _delay_ms(5000);
            y=temp_read();
            y = (y * 5000)/ 256;
            y = y/10;
        }
        else if(y==30)
        {
            PORTC=0b00000000; //turn off the buzzer
        }
        else
        {
            //do nothing
        }
    }
}

SmokeCheck.c

#include "type.h"
#include "avr/io.h"
#define F_CPU 16000000
#include "avr/delay.h"
#include "smoke.h"
void Smokecheck (void)
{
    u32 z;
    somke_init();
    while(1)
    {
        z=smoke_read();

        if (z>600)
        {
            PORTC = 0b00000001;  //turn on the buzzer
            _delay_ms(500);
            PORTC = 0b00000000;  //turn off the buzzer
            _delay_ms(5000);
            z=smoke_read();

        }
        else if(z==30)
        {
            PORTC=0b00000000; //turn off the buzzer
        }
        else
        {
            //do nothing
        }
    }
}

uart.c

#include"avr/io.h"
#include"avr/delay.h"
#include"type.h"
void UART_voidInit(void)
{
    UCSRB=0b00011000;
    UCSRC=0b00000110;
    UBRRL=103;
}
void UART_voidSendByte(u8 Byte)
{
    while(!(UCSRA&(1<<5)));
    UDR=Byte;
}
u8 UART_u8RecievedByte(void)
{
    while(!(UCSRA&(1<<7)));
    return UDR;
}

temp.c

#include "type.h"
#include"avr\io.h"
void temp_init (void)
{
    ADMUX = 0b01100000; //use channel adc0
    ADCSRA = 0b10010000;
}
u8 temp_read (void)
{
    // Strat conversion
    ADCSRA = ADCSRA | 0b01000000;

    // wait till conversion finish
    while ((ADCSRA & 0b00010000) == 0b00000000)
    {

    }
    // clear Flag
    ADCSRA = ADCSRA | 0b00010000;
    // return conversion result
    return (ADCH);

}

smoke.c

#include "type.h"
#include"avr\io.h"
void somke_init (void)
{
    ADMUX = 0b01100001; //use channel adc1
    ADCSRA = 0b10010000;
}
u8 smoke_read (void)
{
    // Strat conversion
    ADCSRA = ADCSRA | 0b01000000;

    // wait till conversion finish
    while ((ADCSRA & 0b00010000) == 0b00000000)
    {

    }
    // clear Flag
    ADCSRA = ADCSRA | 0b00010000;
    // return conversion result
    return (ADCH);

}

type.h

typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned long int u32;
typedef signed char s8;
typedef signed short int s16;
typedef signed long int s32;
typedef float f32;
typedef double f64;

UART_interface.h

void UART_voidInit(void);
void UART_voidSendByte(u8 Byte);
u8 UART_u8RecievedByte(void);

direction.h

void motor (void);

temp.h

void temp_init (void);
u8 temp_read (void);

TempCheck.h

void Tempcheck (void);

smoke.h

void somke_init (void);
u8 smoke_read (void);

SmokeCheck.h

void Smokecheck (void);
\$\endgroup\$
  • 2
    \$\begingroup\$ That the individual components are working does not mean that everything together will just magically work. Most often it won't work so get used to that. Instead of combining everything at once, combine 2 or 3 functions and make that work. Then add a 4th and make it work with the part that works. Continue until all functions are in there. Yes that is a lot of work and a pain, only brilliant or extremely lucky people can make complex things work first time. The rest of us have to take small steps. \$\endgroup\$ – Bimpelrekkie Sep 5 '17 at 9:56
  • 2
    \$\begingroup\$ This is far too broad a question, nobody will be able to answer this unless you can narrow it down to some specific part which isn't working - and what that's not working. \$\endgroup\$ – Lundin Sep 5 '17 at 10:01
  • 2
    \$\begingroup\$ Try separating the code and only loading different bits of it at a time. Connect the components related to that bit of code and see what happens. You have to go step by step to determine the problem. There's not much any of us can do without seeing it, so don;t expect a magical answer. You will have to do as I suggested, bit by bit. If none of it is working, then either you haven't connected things correctly or your code simply isn't right \$\endgroup\$ – MCG Sep 5 '17 at 10:11
  • 5
    \$\begingroup\$ Each of your major functions involves a never ending while loop. It should be obvious from reading the code that the first one called will never exit. \$\endgroup\$ – Roger Rowland Sep 5 '17 at 11:21
  • 2
    \$\begingroup\$ Learn troubleshooting skills. You have written some blocking code. You really want to know what loop the processor is in when it "does nothing". Can you think of a way to indicate (to you, as a debugger) what steps have been done successfully? or what step is currently being executed? \$\endgroup\$ – glen_geek Sep 5 '17 at 11:31
0
\$\begingroup\$

I assume school means K-12 and not university, so I shall try to be more helpful than some of the comments. Basically your code is wrong, and Roger has said where the problem is. Write simple code that tests what happens when the code says

while(1)
{
}

I would suggest

while(1)
{
Serial.print("Message A\n");
}
Serial.print("Message B\n");

then after you understand this behaviour act out what your code does using pen and paper.

\$\endgroup\$
  • \$\begingroup\$ Thanks alot ,I take another code works with my friend ,and romve all the while loop except that in main.c but still my problem the same no syntax error but none of my project hardware is work , I really need to know why it doesn't work Hint: the code I took from my friends is the same as mine in direction.c and UART.c but different in temp.c and smoke.c , I understood their code and half of their code same as mine , So I need to know what's the problem ? \$\endgroup\$ – Sara Gero Sep 8 '17 at 7:36
0
\$\begingroup\$

It is difficult to provide any other suggestions without knowing what changes have been made to the code.

You will need to add debugging information to the code. I would suggest that if you can, then use the serial port to send characters from the robot to the Bluetooth device. If you are using a prewritten Bluetooth app that does not display the return characters, then you may have to use the buzzer to send morse code patterns. When you enter each sub-routine then either send a character to the Bluetooth device, or play a distinct pattern of beeps.

If you have removed all of the while(1) then the answer from glen-geek is relevant. You still have four pieces of blocking code. For example, while loops such as
while ((ADCSRA & 0b00010000) == 0b00000000)
which is a blocking loop waiting for a valid analogue-to-digital conversion.

One of these blocking loops is most likely the problem. Quite probably due to mis wiring, e.g. if the serial input is not connected then the read serial character loop will wait forever.

By the way, for good style, you really should move the fire alarm (?) sound into its own function. And call the sound function from the main loop.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.