# i'm using atmega32 to do an automatic railway gate project i wrote the code ,but i have a problem with it that it doesn't give me the required output? [closed]

I've a school project "automatic railway system ", i wrote the code but it works only on protues . when i burn it on the kit it doesn't work . what is the wrong with this code ??

"my project idea is to make a railway train gate control.

1-when the train is coming make the buzzer on with 90 sec count down on 7-seg. then open the gate.

2- after the train leaving the station close the gate & the buzzer . i'm using 2 limit switches one for opening the gate & the other for closing it. and a dc motor to move the gate between position 0 & position 90. "

but i'm facing an error in seven_seg.c file : expected expression before '=' token in this line

PORTB=PORTB|BUZZER;

i tried to do this to see what happen if i make //PORTB=PORTB|BUZZER; the same error still appears but on this lines in railway_control.c file

if((PINA & SWITCH1)==0b00000000)
PORTB=PORTB | MOTORR;
PORTB=PORTB & ~ BUZZER;
PORTB=PORTB & ~ MOTORR;
else if((PINA & SWITCH2)==0b00000000)
PORTB=PORTB & ~ MOTORL;


what identifiers missing before '=' ?

here is the code after i edited it

#include"avr/io.h"
#define F_CPU 16000000
#include"avr/delay.h"
#include"types.h"
#include"seven_seg.h"

void main (void)
{
DDRA=0b00000000;// define switches as input
PORTA=0b00000011;
DDRB=0b00000111;//define motor & buzzer as output
DDRD=0b11111111;/* using port d &port c for 7-seg*/
DDRC=0b11111111;
while (1)
{
if((PINA&SWITCH1)==0b00000000)
{
display();
PORTB=PORTB|MOTORR;
_delay_ms(3000);
PORTB=PORTB & ~ BUZZER;
PORTB=PORTB&~MOTORR;
_delay_ms(300);
}
else if((PINA&SWITCH2)==0b00000000)
{

PORTB=PORTB|MOTORL;
_delay_ms(3000);
PORTB=PORTB &~ MOTORL;
_delay_ms(300);
}
else
{
//Do Nothing
}

}
}


the seven_seg.c file

#include"avr\io.h"
#include"types.h"
#include"avr/delay.h"
s8 i,j;
s8 arr[10]={0b00111111,0b00000110,0b01011001,
0b01001111,0b01101100,0b01101101,0b01111101,
0b00000111,0b01111111,0b01101111};
void display(void)
{
PORTC=arr[9];
PORTD= arr[0];

PORTB=PORTB|BUZZER;
for(i=8;i>=0;i--)
{
PORTC=arr[i];
for(j=9;j>=0;j--)
{
PORTD=arr[j];
_delay_ms(100);
}
}
}


types.h file

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;
#define BUZZER=0b00000100;
#define MOTORR=0b00000001;
#define MOTORL=0b00000010;
#define SWITCH1=0b00000001;
#define SWITCH2=0b00000010;


seven_seg.h file

#ifndef seven_SEG_H_
#define seven_SEG_H_

void display(void);

#endif /* seven_SEG_H_ */

• What is the project supposed to do, and what is it actually doing? – CHendrix Sep 26 '16 at 17:33
• A tip:- your code will be much easier to read and debug if you use symbolic names instead of 'magic' numbers like 0b11111011. So do eg. #define BUZZER = 0b00000100; PORTB=PORTB&~BUZZER; – Bruce Abbott Sep 26 '16 at 18:08
• Side note, not sure what the default on avr's is. But have you fed the watchdog? – user3219864 Sep 26 '16 at 19:04
• SaraGero - Please note that saying "it doesn't work" is useless :-( There isn't enough detail about what your project should do, what it actually does, and what troubleshooting you've already done. Supplying code is not enough (unless you are very lucky), because we don't know where to look in the code, as you haven't told us what problem to look for! Or you might have a hardware problem (then seeing the code is useless). Please learn how to ask good questions. Although not perfect, read "How To Ask Questions The Smart Way". – SamGibson Sep 26 '16 at 19:10
• @Vicente Cunha I was trying to keep it simple and obvious for a beginner (your way is more flexible and is how I often do it, but obfuscates more). – Bruce Abbott Sep 26 '16 at 19:50

## 2 Answers

when i burn it on the kit it doesn't work .

what is the wrong with this code ??

Who knows? Could be anything. You haven't told us how you know it doesn't work. But that is not relevant, I will tell you why. I will not address your particular code in my answer more than this: It is too big.

It seems to me that this is your first project tested on real hardware. To get this working as quickly as possible, you need to remove everything in your code and start with the smallest code you can debug: The LED blink. This is the Hello, World! of the embedded world.

Your only goal is to get a single pin, or every pin on a whole port, to toggle repeatedly. The speed does not matter if you have an oscilloscope. If you can't even get that to work, then your problem is still easier, because you don't have all that control flow logic to think about.

After you have your pin toggling, you can start to add functionality, carefully, while verifying that every change does what you planned.

• i'm using atmega32 i forgot to mention it .. actually i tried to burn it on the kit nothing of the code work except the motor it give me a strange rotation which i didn't tell him to do this :\ i will try to make the code more smaller than this one – Sara Gero Sep 26 '16 at 18:58
1. There is an extra right parenthesis here:- if ((PINA&0b00000001)==0b00000000))

2. when the switch is pressed you display the 90 second countdown and then close the gate. I'm guessing you want to close the gate first, then do display().

3. You only turn the motor on and off for 3ms. Is this enough time for it to move?

4. You appear to have two switches, one closes the gate when pressed and the other opens it when not pressed. However the comments only say 'if the switch is pressed' and 'if the switch isn't pressed', with no indication that there actually two separate switches. So which is it, one switch or two?

The first step in debugging code is to write it in a way that is easy to understand. Use symbolic names instead of 'magic' numbers, eg. instead of PORTB=PORTB&0b11111011; do:-

#define BUZZER = 0b00000100;
...
PORTB=PORTB&~BUZZER;


Now when you look at your code you can see what it is doing without having to read the comments, and since the symbol is defined only once at the beginning it's harder to make silly mistakes (like changing a pin function and forgetting to edit every instance of it, or typing a binary number incorrectly.)

Comments should clearly and accurately explain what the code does, not just parrot it. // if the switch isn't pressed doesn't tell you which switch is pressed, or what its function is. If you change the code then you should review the comments to make sure that they are still relevant (another reason to make your code 'self documenting' by using appropriate symbolic names).

If a few words aren't enough to describe the circuit then draw one! The code may be perfect, but it still won't work if the hardware doesn't match. Check all your wiring to make sure that things are connected to the correct pins of the MCU. Then use a multimeter to verify the input and output signals.

• actually i use 2 switch one for closing the gate and the other for opening it i use function display before closing the gate becuase i want to give people an alarm that the train is coming but i will try your comment i need to move the gate between angle 0 & angle 90 on a Dc motor so i try to change the delay time but i couldn't reach this point & i try to use a servo motor instead of dc motor but i didn't understand the code i searched on... – Sara Gero Sep 26 '16 at 21:07
• @Sara Gero a hobby servo could be a good choice for this job. It needs a pulse with duration of 1~2ms to set its position, repeated every 20ms until it has moved there (0.2~0.5s). Edit your question to fully describe how the circuit is supposed to work and what it does wrong, and hopefully it will be re-opened. – Bruce Abbott Sep 26 '16 at 21:19