Skip to content

Instantly share code, notes, and snippets.

@eduardodx
Created January 27, 2012 18:37
Show Gist options
  • Save eduardodx/1690222 to your computer and use it in GitHub Desktop.
Save eduardodx/1690222 to your computer and use it in GitHub Desktop.
Arduino - Elevador - Master
#include <Wire.h>
int botoesEstado[4], portasEstado[5], movimentacaoEstado[3], andar = 0;
int chamadasExternas[4], chamadasInternas[4], reqAbrir, reqFechar, emergencia = 0;
int listaPrincipal[4], listaEspera[4] = {0};
void setup () {
Wire.begin(); // Define como Master
}
void botoesP() {
// Iniciar transmissao com ARD#2
Wire.beginTransmission( 2 );
// Requisitar do ARD#2 11 bytes
Wire.requestFrom( 2, 11 );
// Receber informacoes de botoes pressionados
for( int i = 0; i < 11; i++ ) {
botoesEstado[i] = Wire.read();
}
Wire.endTransmission();
for( int i = 0; i < 4; i++ )
chamadasInternas[i] = ( botoesEstado[i] == 1 ) ? i : 0;
for( int i = 4; i < 8; i++ )
chamadasExternas[i] = ( botoesEstado[i] == 1 ) ? i - 4 : 0;
reqAbrir = botoesEstado[8];
reqFechar = botoesEstado[9];
emergencia = botoesEstado[10];
for( int i = 0; i < 4; i++ ) {
listaPrincipal[i] = chamadasInternas[i];
listaEspera[i] = chamadasExternas[i];
}
// Botao de Emergencia Acionado
if( emergencia == 1 ) {
movimentacaoA( movimentacaoEstado[0] );
portasA( 0, 0 );
portasA( 1, 0 );
portasA( 2, 0 );
portasA( 3, 0 );
portasA( 4, 0 );
while(1){};
}
}
void botoesA() {
// Iniciar transmissao com ARD#2
Wire.beginTransmission( 2 );
// Enviar posicao do andar
Wire.write( andar );
// Enviar estado das portas (aberto/fechado)
for( int i = 1; i < 5; i++ ) {
Wire.write( portasEstado[i] );
}
Wire.endTransmission();
}
void portasP() {
// Iniciar transmissao com ARD#3
Wire.beginTransmission( 3 );
// Requisitar do ARD#3 4 bytes
Wire.requestFrom( 3, 5 );
// Pegar estado das portas
for( int i = 0; i < 5; i++ ) {
portasEstado[i] = Wire.read();
}
Wire.endTransmission();
}
void portasA( int porta, int estado ) {
int msg[2];
msg[0] = estado;
msg[1] = porta;
// Inicia transmissao com ARD#3
Wire.beginTransmission( 3 );
// Envia estado das Portas
for( int i = 0; i < 2; i++ ) {
Wire.write( msg[i] );
}
Wire.endTransmission();
}
void movimentacaoP() {
// Inicia transmissao com ARD#4
Wire.beginTransmission( 4 );
// Requisitar do ARD#4 3 bytes
Wire.requestFrom( 4, 3 );
// Pegar dados da movimentacao
for( int i = 0; i < 3 ; i++ ) {
movimentacaoEstado[i] = Wire.read();
}
Wire.endTransmission();
}
void movimentacaoA( int andarReq ) {
// Inicia transmissao com ARD#4
Wire.beginTransmission( 4 );
// Envia andar destino
Wire.write( andarReq );
Wire.endTransmission();
}
int empty( int n[8], int s ) {
int i = 0;
for( i = 0; i < s; i++ ) {
if( n[i] != 0 ) {
return 0;
}
}
return 1;
}
int pegaMaior( int v[4], int l ) {
int res = 0;
for( int i = 0; i < 4; i++ ) {
if( v[i] > res && v[i] < l ) {
res = v[i];
}
}
return res;
}
int pegaMenor( int v[4] ) {
int res = 4;
for( int i = 0; i < 4; i++ ) {
if( v[i] < res ) {
res = v[i];
}
}
return res;
}
void loop () {
// Pegar informacoes dos botoes (ARD#2)
// 4 peinel externo + 6 painel interno (4 andar, abrir e fechar porta) + 1 emergencia
botoesP();
// Pegar informacoes das portas (ARD#3)
// 5 portas
portasP();
// Pegar informacoes das informacoes (ARD#4)
// Sentido Movimentacao + Posicao + Falta Fase
movimentacaoP();
/* Resolve Lista Principal */
while( !empty(listaPrincipal, 4) ) {
int destino = 0;
for( int i = 0; i < 4; i++ ) {
movimentacaoP();
int maior = pegaMaior( listaPrincipal, movimentacaoEstado[1] );
int menor = pegaMenor( listaPrincipal );
if( listaPrincipal[i] != 0 ) {
// Definir um DEFINE para sentido
if( movimentacaoEstado[0] == 2 && maior) {
destino = maior;
for( int i = 0; i < 4; i++ ) {
listaPrincipal[i] = ( listaPrincipal[i] == maior ) ? 0 : listaPrincipal[i];
}
} else if( movimentacaoEstado[0] == 1 && menor ) {
destino = menor;
for( int i = 0; i < 4; i++ ) {
listaPrincipal[i] = ( listaPrincipal[i] == menor ) ? 0 : listaPrincipal[i];
}
} else {
destino = maior;
}
}
}
movimentacaoA( destino );
do {
movimentacaoP();
} while( movimentacaoEstado[0] != 0 );
portasA( destino, 1 );
do {
portasP();
} while( portasEstado[destino + 1] == 1 || portasEstado[0] == 1 );
delay(5000);
botoesP();
}
/* Resolve Lista Espera */
while( !empty(listaEspera, 4) ) {
int destino = 0;
for( int i = 0; i < 4; i++ ) {
if( listaEspera[i] != 0 ) {
destino = listaEspera[i];
listaEspera[i] = 0;
}
}
movimentacaoA( destino );
do {
movimentacaoP();
} while( movimentacaoEstado[0] != 0 );
portasA( destino, 1 );
do {
portasP();
} while( portasEstado[listaEspera[0] + 1] == 1 || portasEstado[0] == 1 );
delay(5000);
botoesP();
if( !empty(listaPrincipal, 4) ) {
break;
}
}
}
@merath
Copy link

merath commented Feb 11, 2013

Eduardo boa noite sou aluno do curso tec automação ind senai rj e estamos na fasr de projeto final, e nossa tarefa e criar um elevador com 4 andares e como desafio isso no arduino matéria que nao tivemos no curso mas e um desafio e gostariamos de fazer vc poderia me ajudar com ideias nao cola, ja que vc desenvolveu um projeto similar

grato

21 33411131
autotec.br.fabiomerath@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment