Como comunicar dos arduinos ?

Hola a todos,

Igor, finalmente la RS485 esta funcionando ;D pero parcialmente. Me explico.

Tengo un amigo que se compro una casa y la casa nueva tiene una piscina. El me preguntó si podía ayudarle a poner una iluminación diferente en la piscina y yo prontamnete empeze a trabajar en este proyeto.

La idea es utilizar un dip switch durantre el prototipo (interruptores en el proyeto final) através de "arduino standalone" con comunicación RS485 comandar los leds RGB en los focos de la piscina.

Cuando cambié los 75176 con el MAX485, my circuito empezo a funcionar pero todavia tengo dos problemas:

Para leer el dip switch con los ascII sugeridas en tu programa ( 48 49 50 etc) solo llegó hasta 9 (que es igula a 57) pero el dip switch que estoy utilizando llega a 15.

Puedo utilizar outros valores abajo de 48 sin problemas? (arriba de 66 el ckecksum es siempre el mismo)

Hecha una ojeada en las modificaciones que hecho para el master, me funciona perfectamente.

Maestro:

//----------------------------------
//RS 485
//By Igor Real
//24-06-09
//----------------------------------


byte            data[12];
unsigned long   previous_time;
unsigned long   previous_time2;
byte            times_repeat=5;
byte            times_repeat2=5;
int             dipState = 0;
byte            state=0;
byte            state2=0;
int             lastdipState = 20;
//pinos dos dips

const int Dip1 = 5;
const int Dip2 = 6;
const int Dip3 = 7;
const int Dip4 = 8;



#define  pinCONTROL    02
#define  myaddress     01
  

void setup() {

   pinMode(13,OUTPUT);
   pinMode(pinCONTROL,OUTPUT);
   //digitalWrite(13,HIGH);
   //digitalWrite(12,LOW);
   //pinMode(8,INPUT);
   //pinMode(9,INPUT);
   digitalWrite(pinCONTROL,LOW);
   // inicializar botoes input:
  pinMode(Dip1, INPUT); 
  pinMode(Dip2, INPUT);
  pinMode(Dip3, INPUT);
  pinMode(Dip4, INPUT);  
   
   Serial.begin(9600);
   //Serial.println("Empezamos");
   state=0;
   state2=0;
}

void loop()
{
ledip();
  //if (digitalRead(8)==state){
   // state=!state;
   // times_repeat=0;  
 //}
 // if (digitalRead(9)==state2){
 //   state2=!state2;
 //   times_repeat2=0;  
 // }

  //if (dipState!=state){
  //  dipState=state;
  //  times_repeat=0;
  //}
  
  if (dipState != lastdipState) {
    // se o botao mudou, incrementa contador
    //Serial.println(dipState); //debug
    // salva o staus do botao, 
    lastdipState = dipState;
    times_repeat=0;
    //Serial.println(dipState);
  }
  
  
  
  if (times_repeat<4){
    Serial.flush();  
    //(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4)
    //Serial.println();
    //Serial.println(dipState);
    
    if (dipState==0){  
      sendMSG(48,50,68,48,48,32,48,48,48,48);
    }
    if (dipState == 1){
      
      sendMSG(48,50,68,48,48,32,48,48,48,49);
    }
     if (dipState == 2){
      
      sendMSG(48,50,68,48,48,32,48,48,48,50);
    }
     if (dipState == 3){
      
      sendMSG(48,50,68,48,48,32,48,48,48,51);
    }
     if (dipState == 4){
      
      sendMSG(48,50,68,48,48,32,48,48,48,52);
    }
     if (dipState == 5){
      
      sendMSG(48,50,68,48,48,32,48,48,48,53);
    }
     if (dipState == 6){
      
      sendMSG(48,50,68,48,48,32,48,48,48,54);
    }
     if (dipState == 7){
      
      sendMSG(48,50,68,48,48,32,48,48,48,55);
    }
     if (dipState == 8){
      
      sendMSG(48,50,68,48,48,32,48,48,48,56);
    }
     if (dipState == 9){
      
      sendMSG(48,50,68,48,48,32,48,48,48,57);
    }
     if (dipState == 10){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,20);
    }
     if (dipState == 11){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,21);
    }
     if (dipState == 12){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,22);
    }
     if (dipState == 13){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,23);
    }
     if (dipState == 14){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,24);
    }
     if (dipState == 15){
      
      //sendMSG(48,50,68,48,48,32,48,48,48,25);
    }
     
     
     
     
    
    times_repeat=times_repeat+1;
  
    previous_time=millis();
    while (((millis()-previous_time)<500) && (Serial.available()!=15)){
      ;;
    }
  
    if (Serial.available()>=15){
      if (receiveMSG()==1){
        //Serial.println("Trama correcta");
        if (data[0]==48 && data[1]==49 && data[2]==6){
          //ACK  
          times_repeat=5;
          //Serial.println("ACK recibido");
        }
      }
     }
   }  
  
  

  //if (times_repeat2<4){
   // Serial.flush();  
    //(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4)
  //  if (digitalRead(9)==HIGH){  
  //    sendMSG(48,50,68,48,48,32,48,48,48,49);
  //  }else {
  //    sendMSG(48,50,68,48,48,32,48,48,48,48);
  //  }
  //  times_repeat2=times_repeat2+1;
  
  //  previous_time2=millis();
  //  while (((millis()-previous_time2)<500) && (Serial.available()!=15)){
  //  ;;
  //  }
  
  //  if (Serial.available()>=15){
  //    if (receiveMSG()==1){
       //Serial.println("Trama correcta");
  //      if (data[0]==48 && data[1]==65 && data[2]==6){
          //ACK  
   //       times_repeat2=5;
          //Serial.println("ACK recibido");
   //     }
   //   }
  //  }
 // }
  
  
  
}


//------------------------
//FUNCIONES
//------------------------

void ledip()
{
  dipState=digitalRead(5) + (digitalRead(6)<<1) + (digitalRead(7)<<2) + (digitalRead(8)<<3);
  //delay(5);
  
}


byte receiveMSG(){

  byte  byte_receive;
  byte  state=0;
  byte  cont1=1;
  byte  trace_OK=0;

  unsigned int checksum;
  unsigned int checksum_trace;
  
  
  
  while (Serial.available() > 0){
    
     byte_receive=Serial.read();
     if (byte_receive==00){
       state=1;
       checksum_trace=0;
       checksum=0;
       trace_OK=0;
       cont1=1;
     }else if (state==1 && cont1<=12){
       data[cont1-1]=byte_receive;
       checksum=checksum+byte_receive;
       cont1=cont1+1;
     }else if (state==1 && cont1==13){
       checksum_trace=byte_receive<<8;
       cont1=cont1+1;
     }else if (state==1 && cont1==14){
       checksum_trace=checksum_trace+byte_receive;
       cont1=cont1+1;
       state=0;
       if (checksum_trace==checksum){
           trace_OK=1;
       }else{
         trace_OK=0;
       }
       break;
     }
  }
  return trace_OK;

}



void sendMSG(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4){
  
  unsigned int checksum_ACK;
  checksum_ACK=address1+address2+5+data_type+code1+code2+Sign+data1+data2+data3+data4+3;
  
  UCSR0A=UCSR0A |(1 << TXC0);
  
  digitalWrite(pinCONTROL,HIGH);
  delay(1);

  Serial.print(0,BYTE);
  Serial.print(address1,BYTE);
  Serial.print(address2,BYTE);
  Serial.print(5,BYTE);
  Serial.print(data_type,BYTE);
  Serial.print(code1,BYTE);
  Serial.print(code2,BYTE);
  Serial.print(Sign,BYTE);
  Serial.print(data1,BYTE);
  Serial.print(data2,BYTE);
  Serial.print(data3,BYTE);
  Serial.print(data4,BYTE);  
  Serial.print(3,BYTE);
  Serial.print(((checksum_ACK>>8)&255),BYTE);
  Serial.print(((checksum_ACK)& 255),BYTE);
  while (!(UCSR0A & (1 << TXC0)));
  digitalWrite(pinCONTROL,LOW);

  
  
}



void sendACK(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4){
  
  unsigned int checksum_ACK;
  checksum_ACK=address1+address2+6+data_type+code1+code2+Sign+data1+data2+data3+data4+3;
  
  UCSR0A=UCSR0A |(1 << TXC0);
  
  digitalWrite(pinCONTROL,HIGH);
  delay(1);

  Serial.print(0,BYTE);
  Serial.print(address1,BYTE);
  Serial.print(address2,BYTE);
  Serial.print(6,BYTE);
  Serial.print(data_type,BYTE);
  Serial.print(code1,BYTE);
  Serial.print(code2,BYTE);
  Serial.print(Sign,BYTE);
  Serial.print(data1,BYTE);
  Serial.print(data2,BYTE);
  Serial.print(data3,BYTE);
  Serial.print(data4,BYTE);  
  Serial.print(3,BYTE);
  Serial.print(((checksum_ACK>>8)&255),BYTE);
  Serial.print(((checksum_ACK)&255),BYTE);
  while (!(UCSR0A & (1 << TXC0)));
  digitalWrite(pinCONTROL,LOW);
  
  
}

void sendNAK(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4){
  
  unsigned int checksum_ACK;
  checksum_ACK=address1+address2+6+data_type+code1+code2+Sign+data1+data2+data3+data4+3;
  
  UCSR0A=UCSR0A |(1 << TXC0);
  
  digitalWrite(pinCONTROL,HIGH);
  delay(1);

  Serial.print(0,BYTE);
  Serial.print(address1,BYTE);
  Serial.print(address2,BYTE);
  Serial.print(15,BYTE);
  Serial.print(data_type,BYTE);
  Serial.print(code1,BYTE);
  Serial.print(code2,BYTE);
  Serial.print(Sign,BYTE);
  Serial.print(data1,BYTE);
  Serial.print(data2,BYTE);
  Serial.print(data3,BYTE);
  Serial.print(data4,BYTE);  
  Serial.print(3,BYTE);
  Serial.print(((checksum_ACK>>8)&255),BYTE);
  Serial.print(((checksum_ACK)&255),BYTE);
  while (!(UCSR0A & (1 << TXC0)));
  digitalWrite(pinCONTROL,LOW);
  
  
}



byte hex2num(byte x){

  byte result;
  
  if (x>=48 && x<=57){
    result=x-48;  
  }else if (x>=65 && x<=70){
    switch(x){
      case 65:
        result=10;
        break;
      case 66:
        result=11;
        break;
      case 67:
        result=12;
        break;
      case 68:
        result=13;
        break;
      case 69:
        result=14;
        break;
      case 70:
        result=15;
        break;    
    }  
  }
  return result;  
}