global Variable in Cpp file

hello
I have this funtion in cpp file:

#include "RS485.h"
int controlBusDirection;
void controlPin(int controlBusPin)
{
  controlBusDirection = controlBusPin ;
  Serial.print(controlBusPin);//debug
  Serial.print(controlBusDirection);//debug
  pinMode(controlBusPin, OUTPUT);//Pin that Control Data Direction
  digitalWrite(controlBusPin,HIGH);//Enable Transmitter,Disable Receiver on default state
}

in the heder file I have:

#ifndef RS485_H
#define RS485_H
#include <Arduino.h>

void controlPin(int controlBusPin);

In the main skecth this:

#include "RS485.h"
#define startByte 0x01
#define masterID 0x32

byte dataSent[6]; //start byte , ID, function,message1,message2,CRC
byte dataReceived[6];  //start byte , ID return, function return ,message1 return,message2 return,CRC
uint8_t pino = 13;
void setup() 
{   
  Serial.begin(9600);
  controlPin(13);
}
void loop() 
{

}

The problem is if I have this line on the cpp file "controlBusDirection = controlBusPin ;" it does not put High pin 13.
If I remove it works.
I cant see why?
Any help

put:

"int controlBusDirection;"

in the header file. I think that will fix it. I could be wrong though. Doesn't do any harm to try.

I test that but not work.This time I get a compile error./arduino-1.0.1/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
Is it allowed to have global variables in the header file?
I just put it this way

#ifndef RS485_H
#define RS485_H

#include <Arduino.h>


int controlBusDirection = 0;//Global 

void controlPin(int controlBusPin);

Sorry, I made a mistake.

leave in the cpp file:

int controlBusDirection = 0;//Global

and put this in the h file:

extern int controlBusDirection;

I done that but the problem is the same.

void controlPin(int controlBusPin)
{
  controlBusDirection = controlBusPin ;//The problem is here.If this line is commented pinMode and digitalWrite works
  Serial.print(controlBusPin);//debug
  Serial.print(controlBusDirection);//debug
  pinMode(controlBusPin, OUTPUT);//Pin that Control Data Direction
  digitalWrite(controlBusPin,HIGH);//Enable Transmitter,Disable Receiver on default state
}

I'm getting crazy to discover what is happening here.On the Serialprint it prints the right pin number passed to the function (13 in my test) It was suppose to set the output and then switch it to High.Even if something was wrong with the global variable the value is not destroyed on local variable controlPin I believe.

Its not a code issue. I tried out the code and it works fine. :~

Did you still have your test sketch?I want upload it here.I know this could sound strange but if you say it works I just want make sure it is something wrong with my code that I am uploading here.A already have made some changes :astonished:

The only difference between the code I used and the code you posted at the beginning was that there is a #endif missing at the end of this block, but it wouldn't even compile without it, so I presume you just forgot to post that.

#ifndef RS485_H
#define RS485_H
#include <Arduino.h>

void controlPin(int controlBusPin);

I have attached the sketch I used.

test.zip (838 Bytes)

You are right.It was all about a while loop I have in the program.With your test I compare it and I found the problem.
For get more small and fit on the first post I omit that part since I don't ever believe the cause was there.I get stuck on just global variable with full attention.
You save me a lot of time.I'm tracking it for about 4 hours :grin:
Thanks TCWORLD

General rule of thumb with programming:

9 times out of 10 the bug is in the bit you're certain isn't the problem :smiley: