Test Version 4

parent cf6f86fb
#include <Arduino.h>
//ver se os 15 fazem interferencia
#define MASTER_ADDR 15
#define SLAVE1_ADDR 13
......@@ -35,7 +37,7 @@ void asynch9_init(long BAUD) {
UBRR0L = (unsigned char)ubrr; // 8 bits menos significativos
/*Set transmitter*/
if(ADDR & MASTER_ADDR){
if(ADDR == MASTER_ADDR){
UCSR0B |= (1<<TXEN0);
}
else /*Set Receiver*/{
......@@ -90,8 +92,8 @@ uint8_t get_data(uint8_t *data) {
}
void setup() {
// put your setup code here, to run once:~
int ADDR_PINS [4] = {0};
// put your setup code here, to run once:
long BAUD = 9600;
pinMode(ADDR3_PIN, INPUT_PULLUP);
......@@ -103,14 +105,11 @@ void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(WREN_PIN, OUTPUT);
ADDR_PINS[0] = digitalRead(ADDR0_PIN);
ADDR_PINS[1] = digitalRead(ADDR1_PIN);
ADDR_PINS[2] = digitalRead(ADDR2_PIN);
ADDR_PINS[3] = digitalRead(ADDR3_PIN);
digitalWrite(LED_PIN,LOW);
ADDR = ADDR_PINS[3]<<3 | ADDR_PINS[2]<<2 | ADDR_PINS[1]<<1 | ADDR_PINS[0] ;
ADDR = (digitalRead(ADDR3_PIN)<<3) | (digitalRead(ADDR2_PIN)<<2) | (digitalRead(ADDR1_PIN)<<1) | (digitalRead(ADDR0_PIN)) ;
asynch9_init(BAUD);
}
......@@ -119,35 +118,35 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
// Master
if(ADDR & MASTER_ADDR){
if(ADDR == MASTER_ADDR){
digitalWrite(WREN_PIN,HIGH); // para o transciever do master possa enviar
buttonState1 = digitalRead(BUT1_PIN);
buttonState2 = digitalRead(BUT2_PIN);
if(buttonState1 != lastButtonState1){
if(buttonState1 == LOW){
if(last_ADDR & SLAVE1_ADDR){
if(last_ADDR == SLAVE1_ADDR){
send_data(15);
} else {
send_addr(SLAVE1_ADDR);
send_data(0);
send_data(15);
last_ADDR = SLAVE1_ADDR;
}
} lastButtonState1 = buttonState1;
}else if(buttonState1 != lastButtonState1){
}
}else if(buttonState1 != lastButtonState1){ //quando o button é libertado
if(buttonState1 == HIGH){
if(last_ADDR & SLAVE1_ADDR){
if(last_ADDR == SLAVE1_ADDR){
send_data(0);
} else {
send_addr(SLAVE1_ADDR);
send_data(15);
send_data(0);
last_ADDR = SLAVE1_ADDR;
}
}
}
lastButtonState1 = buttonState1;
if(buttonState2 != lastButtonState2){
if(buttonState2 == LOW){
if(last_ADDR & SLAVE2_ADDR){
if(last_ADDR == SLAVE2_ADDR){
send_data(15);
} else {
send_addr(SLAVE2_ADDR);
......@@ -155,32 +154,36 @@ void loop() {
last_ADDR = SLAVE2_ADDR;
}
}
lastButtonState2 = buttonState2;
}else if(buttonState2 != lastButtonState2){
if(buttonState2 == HIGH){
if(last_ADDR & SLAVE2_ADDR){
send_data(0);//o que é que ele envia para o led ficar a 1?
if(last_ADDR == SLAVE2_ADDR){
send_data(0);
} else {
send_addr(SLAVE2_ADDR);
send_data(0);//nao sei se isso é necessário?
send_data(0);
last_ADDR = SLAVE2_ADDR;
}
}
}
lastButtonState2 = buttonState2;
}
//slaves
if(ADDR & SLAVE1_ADDR || ADDR & SLAVE2_ADDR){
if(ADDR == SLAVE1_ADDR || ADDR == SLAVE2_ADDR){
digitalWrite(WREN_PIN,LOW); // Para que os transceivers (os slaves) possam receber
getData = get_data(data);
if(getData == 1 && ADDR == *data){// se for o endereço
UCSR0A &= ~(1<<MPCM0); //para comecar a "ouvir" data framees
}else if(getData == 1 && ADDR != *data){
UCSR0A |= (1<<MPCM0); //senao for o seu endereco continuar a ouvir "addr"
}
if(*data == 15 && getData == 0)
/* if(*data == 15 && getData == 0)
digitalWrite(LED_PIN, HIGH);
if(*data == 0 && getData == 0)
digitalWrite(LED_PIN, LOW);
*/
if(getData==0)
digitalWrite(LED_PIN,*data);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment