// ---------------------------------------------------------------------------
/* Código para Arduino Mega 2560
Leitura de canal analógico A0 (potenciometro)
jams, EESC/USP, 2024 */
// ---------------------------------------------------------------------------
// Diretivas de pré-processador
#define LED 13
#define ADC 0
// ---------------------------------------------------------------------------
// Declaração de variáveis, protótipos de funções, etc
const int bitsADC = 1023; // arduinoUno, arduinoMega = 2^10 valores (0 - 1023)
const int escalaInf = -50; // mapeio de 0 - 1024 para 230 a -50 (passando pelo 0 graus)
const int escalaSup = 230; // mapeio de 0 - 1024 para 230 a -50 (passando pelo 0 graus)
const int delayMain = 1000; // atraso da função principal
int j = 0; // contador
void delay1s( void );
// ---------------------------------------------------------------------------
// Configuração GPIO periféricos etc
void setup( void ){
// configurar builtin-led pin como saída
pinMode( LED, OUTPUT );
// initialize serial communication at 9600 bits per second:
Serial.begin( 9600 );
// Configuração do ADC:
analogReference( DEFAULT ); // faixa de tensões elétricas de entrada: 0 V - 5 V
}
// ---------------------------------------------------------------------------
// Loop principal
void loop( void ){
// ---------------------------------------------------------------------------
// Declaração das variáveis
int leitura = 0;
int angulo = 0;
int anguloProc = 0;
char mensagem1[9] = "Iteracao";
// ---------------------------------------------------------------------------
// Leitura da tensão analógica no pino A0:
leitura = analogRead( ADC ); // analogRead( pin );
// ---------------------------------------------------------------------------
// Processamento: mapeio dos valores digitais do potenciômetro para valores en graus
angulo = map( leitura, 0, bitsADC, escalaSup, escalaInf );
//
if( angulo < 0 ){
anguloProc = 0;
}
else{
if( angulo > 180 ){
anguloProc = 180;
}
else{
anguloProc = angulo;
}
}
// ---------------------------------------------------------------------------
// Imprimir leitura na porta serial:
j = j + 1;
Serial.print( mensagem1 );
Serial.print( " # " );
Serial.print( j );
Serial.print( " = " );
Serial.print( anguloProc );
Serial.print( "\n" );
//delay( delayMain );
delay1s();
}
// ---------------------------------------------------------------------------
// Atraso de 1 segundo utilizando estrutura de repetição 'for'
void delay1s( void ){
volatile char i;
volatile unsigned int k;
//char i;
//unsigned int k;
digitalWrite( LED, !digitalRead( LED ) );
for( k = 0; k < 14266 ; k++ ){
for( i = 0; i < 100 ; i++ );
}
}