TRAFFIC LIGHT WORKING NOT RIGHT???

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

HI, I TRY TO MAKE A TRAFFIC LIGHT WITH BUTTON FOR WALKER BUT WHEN I USE THE INTERRUPT THEN THE FOLLOWING HAPPEN

1. THE LIGHT NOT ON/ OFF IN SEQUENCY (GREEN & RED ON BUT YELLOW NOT ON???)

2. WHEN I USED UART TO DEBUG (USE PUTTY) I SAW "A" COME UP EVEN I DID NOT PUSH THE BUTTON???

3. WHEN I PRESS BUTTON THE RED LIGHT ON. THIS IS WHAT I WANT 

CAN ANYBODY FIND OUT WHAT IS WRONG WITH IT. 

HERE : DDB0 IS BUTTON INPUT SIGNAL

DDB5 : GREEN LIGHT

DDB3: YELLOW LIGHT

DDB1: RED LIGHT 

 

I USED ATMEGA 328P AND CODE IN C LANGUAGE (IDE ECLIPSE)

 

#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>

ISR (PCINT0_vect)

{

         UDR0='A';// CHK LOI MOI KHI LAM DEN DO SANG SE GUI KEM KI TU A

         PORTB=(1<<DDB1);

 

}

 

void Init_Uart ( ) //CHUONG TRINH KIEM TRA LOI KO LIEN QUAN TOI PROJECT

 {

 // baudrate = 57600

 UBRR0H=0;

 UBRR0L=16;

 

 // enablereceiver and transmitter , Rx complete interrupt enable

 UCSR0B = (1<<RXEN0) |(1<<TXEN0) |(1<<RXCIE0) ;

 // set frame format

 UCSR0C=(1<<USBS0) |(3<<UCSZ00) ;

 //UCSR0C=0x06 ;

 }

 

 int main ( )

 {

        PCMSK0 = 0X01;// PCINT7..0: Pin Change Enable Mask 7..0

        PCICR= 0X01;// PCIE0: Pin Change Interrupt Enable 0

        asm ("sei");// ENABLE GLOBAL INTERRUPT

        

Init_Uart ( );// GOI CHUC NANG UART

 

        DDRB = ((0<<DDB0)|(1<<DDB5)|(1<DDB3)|(1<<DDB1));//531 XVD O PB

       //DDB0: CHAN PCINT0 HAY PB0 HAY DIGITAL PIN8

 

        while (1)

        {

              PORTB|=(1<<DDB5) ; //xanh pin 13 pb5 0X20

              _delay_ms(10000);

              PORTB|=(1<DDB3); //vang pin 11 pb3 0X08

              _delay_ms(1000);

              PORTB|=(1<<DDB1); //do pin 9 pb1 0X02

              _delay_ms(10000);

              PORTB|= (1<<DDB3)|(1<<DDB1);// VANG DO 0X0A

              _delay_ms(1500);

              PORTB = 0;

        }

 return 0 ;

 }

Last Edited: Fri. Mar 8, 2019 - 12:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I question your whole approach to the problem, you may want to take a look at state machines and try that method. 

There is a tutorial on state machines in the tutorial section here:  https://www.avrfreaks.net/forum/...

 

Good luck.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are the words "xanh", "vang", "do" really "red", "yellow", "green" in your language?

 

It's very difficult to follow what this code is attempting to do without knowing details such as this.

 

Anyway the point of this is that you really want a "state machine" for this. Pressing buttons should not directly change the lights but simply change the state.

 

In the UK traffic lights follow the sequence:

 

"Red",

"Amber"

"Green"

"Green+Amber"

 

and then loops back to the top. So you can assign state names to each of these colour combinations. One might envisage something like:

typedef enum {
    STATE_RED,
    STATE_AMBER,
    STATE_GREEN,
    STATE_GREEN_AMBER
} light_e;

typdef struct {
    light_e state;
    uint16_t phase_length_ms;
} phases_t;

phases_t phases[] = {
    { STATE_RED, 1000 },
    { STATE_AMBER, 400 },
    { STATE_GREEN, 1200 },
    { STATE_GREEN_AMBER, 300 }
};

Or something along these lines.

Last Edited: Tue. Mar 5, 2019 - 01:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no do is red and xanh is green ;)   (nice to know before you get into trouble) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Newchance wrote:
GREEN & RED ON BUT YELLOW NOT ON???
Neither do you ever change the state of PortB3, nor is it even configured as output in the first place.

Hint: << and < are two completely different operators.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I second the nomination of State Machine.  A state machine is not so bad for a single (one  direction) stop light. But, when you add lights in the cross direction and pedestrian lights, the number of states starts to go up. Not catastrophically, but more than one might expect. 

 

The important thing, however, is that adding states "costs" you very little in program complexity (for THIS problem, anyway). What does become challenging is defining the different states (that is, which lights are on, and which are off) and what drives the transitions between the states.

 

Its also surprising to me, at least, how many different stop light patterns are in use. In (much of) the U.S., it used to be simple red -> green -> amber -> red. Now, there are flashing turn lane indicators, and lights where all are red at the same time for "scramble" pedestrian crossing and a lot more.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Newchance,

first of all, please stop shouting (writing all capital) that is considered rude.

 

Have a good look at this very small piece of code:

             PORTB|=(1<<DDB5) ; //xanh pin 13 pb5 0X20

              _delay_ms(10000);

              PORTB|=(1<DDB3); //vang pin 11 pb3 0X08

              _delay_ms(1000);

              PORTB|=(1<<DDB1); //do pin 9 pb1 0X02

 

there is a very very small difference between the statements that will make your code not work, as expected only for the yellow light which is on PB3