Two arduino Communicating (SOLVED)

im trying to get an uno to do something when i throw a pin on my mega HIGH. but it doesnt seem to work.

code on mega:

int hot = 10;

void setup() {
  // put your setup code here, to run once:
     pinMode(hot, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(hot, HIGH);
delay(1000);
digitalWrite(hot,LOW);
}

code for UNO:

int receive = 10;
int LED = 13;
void setup() {
  // put your setup code here, to run once:
   pinMode(receive, INPUT);
   pinMode(LED, OUTPUT);
  val = 0;
}

void loop() {
  // put your main code here, to run repeatedly: 
  val = digitalRead(receive);
  if(val == HIGH)
{
 digitalWrite(LED, HIGH);
}
else {digitalWrite(LED, LOW);}
}

Why wont this work?? it works when i run it all on the uno or all on the mega.... but not when i combine the 2.

tht was just a typo n the forum page.. sorry about tht. the code compiles on both arduinos and runs but its almost like the HIGH from the mega doesnt equate to a HIGH on the UNO... could this be possible???

Sorry, i might have been a bit rude there. Are you putting a resistor between the uno and mega pins or just running it straight through?

oh, and i think you need another delay after writing low or it'll just stay high almost all the time

ive tried both actually. i ran a straight wire first. it did nothing.
i now have the wire running from the mega to a breadboard which grounds with a resistor then to the input on UNO

I found this really useful in my project: EasyTransfer Arduino Library « The Mind of Bill Porter

TenderLoins:
I found this really useful in my project: EasyTransfer Arduino Library « The Mind of Bill Porter

this seems to be useful for sending data over serial connection.. but im going for a WAY more simpler answer... just for the UNO to know when the mega throws a pin HIGH... thts all i need. this should be so simple

but for some reason.. ?maybe because uno to mega?.. they dont work seamlessly

did you try putting the second delay in? otherwise the loop just goes to high too quickly to see any change!

yes i did here is a paste of my exact code as in arduin ide
uno

int receive = 12;
int LED = 13;
int   val = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(receive, INPUT);
  pinMode(LED, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly: 
  val = digitalRead(receive);
  if(val == HIGH)
  {
    digitalWrite(LED, HIGH);
    delay(1500);
    val = 0;
  }
  else {
    digitalWrite(LED, LOW);
    delay(1500);
    val = 0;
  }
}

mega

int hot = 23;


void setup() {
  // put your setup code here, to run once:
  pinMode(hot, OUTPUT);
 
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(hot, HIGH);
  delay(9000);
  digitalWrite(hot,LOW);
  delay(9000);
}

i have a wire going from pin 23 to pin 12 with a pull down resistor in-between. thats simple as it gets and its not wrking

Can you describe all the wiring involved, including the power supplies, USB connections (if any) and so on?

I assume it's powered by usb? Maybe the usb bus isn't powering the two devices sufficiently. A quick search says a usb port can give out 100mA. A pin can put out 40mA, so that might be enough to screw it up in addition to running both arduinos and any other devices that are plugged in. Also I would print over serial the values analogread of the input pin returns, just as a test to be sure it's not simply lacking power. I think it takes 2.5v for digitalread to be high, so less than 500 in analogread means you've got a problem. 0 means I don't know what to do. If it is less than 500, switch to an external power source (unless you already did).

Herrow

Not sure if it is of use to your. But the below code is used to recieve and send Wiegand signals which essentially is just sending bits back and forth.

I suggest you look at the interupt functions and research these a bit more. I can pretty much garunty you will have alot more joy from that point on.

Ive included some code below im at work and cannot explain it please advise if you have any questions and i will do my best to answer.

Code with sending an receiving

int i;
int reader1[30];
int readerTmp[30];
volatile int reader1Count = 0;

void reader1One(void) {
  reader1[reader1Count] = 1;
  reader1Count++;
}

void reader1Zero(void) {
reader1[reader1Count] = 0;
  reader1Count++;
}


void setup()
{
  Serial.begin(57600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, FALLING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, FALLING); //DATA1 to pin 3
  delay(10);
  
  //Set pins 10/D0 and 11/D1 to output for re transmission
   pinMode(10, OUTPUT);
   pinMode(11, OUTPUT);
   
   digitalWrite(10, HIGH);
   digitalWrite(11, HIGH);
  
  
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
  pinMode(i, OUTPUT);
   digitalWrite(i, HIGH); // enable internal pull up causing a one
  digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
  pinMode(i, INPUT);
  digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
  // put the reader input variables to zero
  for (i = 0; i < 30; i = i + 1) {
  reader1[i] = 0;
}
  reader1Count = 0;
  digitalWrite(13, HIGH);  // show Arduino has finished initilisation
}

void loop() {
  if(reader1Count >= 27){
  Serial.println("A");
  

readerTmp[0] = reader1[25] ;

readerTmp[1] = 0 ;
readerTmp[2] = 0 ;
readerTmp[3] = 0 ;
readerTmp[4] = 0 ;
readerTmp[5] = 0 ;
readerTmp[6] = 0 ;
readerTmp[7] = 0 ;
readerTmp[8] = 0 ;

readerTmp[9] = 0 ;
readerTmp[10] = 0 ;
readerTmp[11] = 0 ;
readerTmp[12] = 0 ;
readerTmp[13] = 0 ;
readerTmp[14] = 0 ;
readerTmp[15] = 0 ;
readerTmp[16] = 0 ;
readerTmp[17]= 0 ;
readerTmp[18] = 0 ;
readerTmp[19] = 0 ;
readerTmp[20] = 0 ;
readerTmp[21] = 0 ;
readerTmp[22] = 0 ;
readerTmp[23] = 0 ;
readerTmp[24] = 0 ;

readerTmp[25] =  reader1[0] ;

//If sum of segments 2 to 13 even then First parity 1
if ((readerTmp[1] + readerTmp[2] + readerTmp[3] + readerTmp[4] + readerTmp[5] + readerTmp[6] + readerTmp[7] + readerTmp[8] + readerTmp[9] + readerTmp[10] + readerTmp[11] + readerTmp[12]) % 2){
 readerTmp[0] = 1;
  Serial.println("even = odd");
}else{
 readerTmp[0] = 0;
  Serial.println("even = even");
}


//If sum of segments 14 to 25 even then last parity 1
if ((readerTmp[13] + readerTmp[14] + readerTmp[15] + readerTmp[16] + readerTmp[17] + readerTmp[18] + readerTmp[19] + readerTmp[20] + readerTmp[21] + readerTmp[22] + readerTmp[23] + readerTmp[24]) % 2){
 readerTmp[25] = 0;
 Serial.println("odd = odd");
}else{
 readerTmp[25] = 1;
 Serial.println("odd = even");
}

 
for (i = 0; i < 26; i = i + 1) {

  if(readerTmp[i] == 0){
    delay(2);
  digitalWrite(10, LOW);
  delayMicroseconds(50);
  digitalWrite(10, HIGH); 
  }else{
   delay(2);
  digitalWrite(11, LOW);
  delayMicroseconds(50);
  digitalWrite(11, HIGH); 
  }
  
  Serial.print(readerTmp[i]);
  readerTmp[i] = 0;
}

Serial.println("");
  reader1Count = 0;
     }    
}

Code to receive 26bits

/* Crazy People
 * By Mike Cook April 2009
 * Three RFID readers outputing 26 bit Wiegand code to pins:-
 * Reader A (Head) Pins 2 & 3
 * Interrupt service routine gathers Wiegand pulses (zero or one) until 26 have been recieved
 * Then a sting is sent to processing
 */

volatile long reader1 = 0;
volatile int reader1Count = 0;

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;  
}


void setup()
{
  Serial.begin(57600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, FALLING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, FALLING); //DATA1 to pin 3
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
  pinMode(i, OUTPUT);
   digitalWrite(i, HIGH); // enable internal pull up causing a one
  digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
  pinMode(i, INPUT);
  digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  digitalWrite(13, HIGH);  // show Arduino has finished initilisation
}

void loop() {
  if(reader1Count >= 27){
//  Serial.print(" Reader 1 ");Serial.println(reader1,HEX);
  Serial.println("A");
  Serial.println(reader1 & 0xfffffff);
  Serial.println(reader1, DEC);
  Serial.println(reader1, BIN);
  reader1 = 0;
  reader1Count = 0;
     }    
}

Code to consistently send bits 0/1/0/1 etc.....

void setup(){
 pinMode(2, OUTPUT);   
 pinMode(3, OUTPUT); 
 
 digitalWrite(2, HIGH);
 digitalWrite(3, HIGH);
 
 delay(5000);
 
}

void writeD0(){
digitalWrite(10, LOW);
delayMicroseconds(50);
digitalWrite(10, HIGH);

}
void writeD1(){
  digitalWrite(11, LOW);
  delayMicroseconds(50);
  digitalWrite(11, HIGH);
}

void loop(){
  delay(2);
  digitalWrite(2, LOW);
  delayMicroseconds(50);
  digitalWrite(2, HIGH);
  delay(2);
  digitalWrite(3, LOW);
  delayMicroseconds(50);
  digitalWrite(3, HIGH);
}

Agreed. Interrupts are much more elegant. You should also be able to hook the uno's rx to one of the mega's tx lines and just send whatever you want with Serial.write(), which keeps the code short.

lol at you mentioning the usb power issue - i beleive this was causing me issues over a 2 week period in trying to get my code working - then all of a sudden after a bunch of random results i decided to power it off a 12v supply and BAM code started working.

:smiley:

alright guys i got it working.

the power source was the problem. the USb wasnt providig enough power to run the devices i had connected plus give enouigh current to mke the 2nd arduino read HIGH. with a 12v walwor everything is working perfectly.

thanx for all the help.