Olá a todos.
Para ser direto, tenho um problema e gostaria de saber se alguém pode ajudar?!
Gostaria de ligar as portas digitais de (2 a 9) a uma CHAVE BINARIA e depois, com a leitura da combinação destes pinos, enviar os dados serialmente a um display Oled para verificação.
Tenho alguma pratica com PICBASIC e neste ambiente, basta atribuir o (pinport) a um bit determinado em uma variável, depois ler o valor e processa-lo...
EX:
TESTE var BYTE //variável TESTE de 8 bits.
TESTE.0 = portb.0
TESTE.1 = portc.3
TESTE.2 = portd.0
.
.
.
Assim, se precisar trabalhar apenas 5 bits posso manipular a variável TESTE sem afetar diretamente o portb, portc ou mesmo o portd...
Procurei uma atribuição semelhante para o arduíno porem não obtive sucesso...
Agradeço qualquer ajuda.
Não em serialmente. I2C.
OK XPFD quando disse serialmente foi um modo simplório de me referir ao protocolo I2C. Pois, poderia tentar enviar os dados para o LCD em modo de 4 ou 8 bits paralelos...
A DIFICULDADE está em ler o valor BINÁRIO nos pinos mencionados e armazenar em uma variável para tratamento posterior...
Faça uma matriz de 128 elementos. Você só poderá usar Serial.print(); se você usar uma placa com mais memória (OLED falhará)
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_MIDDLE SCREEN_HEIGHT / 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int i, buttonPin = 2;
bool oldButtonState;
float torque[128]; // <-- store 128 values
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 failed"));
while (1);
}
display.clearDisplay();
}
void loop() {
display.drawLine(i, 0, i, 63, BLACK); // verticle line to erase trace
display.display();
if (digitalRead(buttonPin)) {
torque[i] = SCREEN_MIDDLE - 10;
display.drawPixel(i, SCREEN_MIDDLE - 10, WHITE);
} else {
torque[i] = SCREEN_MIDDLE + 10;
display.drawPixel(i, SCREEN_MIDDLE + 10, WHITE);
}
// Serial.print(torque[i]); // Not Uno/Nano. Only works with MEGA2560
display.display();
i++; // next pixel
if (i == 128) { // OLED right edge
i = 0; // reset i
}
}
Meu erro foi usar "float"... este funciona com "int"...
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_MIDDLE SCREEN_HEIGHT / 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int counter, buttonPin = 2, torque[128]; // <-- store 128 values
bool oldButtonState;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 failed"));
while (1);
}
display.clearDisplay();
}
void loop() {
display.drawLine(counter, 0, counter, 63, BLACK); // verticle line to erase trace
display.display();
readButton();
writeBuffer();
}
void readButton() {
if (digitalRead(buttonPin)) {
torque[counter] = SCREEN_MIDDLE - 10;
display.drawPixel(counter, SCREEN_MIDDLE - 10, WHITE);
} else {
torque[counter] = SCREEN_MIDDLE + 10;
display.drawPixel(counter, SCREEN_MIDDLE + 10, WHITE);
}
display.display();
}
void writeBuffer() {
counter++; // next pixel
if (counter == 128) { // OLED right edge
for (int j = 0; j < SCREEN_WIDTH / 16; j++) { // 8 rows
for (int i = 0; i < SCREEN_WIDTH / 8; i++) { // 16 columns
if ((torque[j * 8 + i]) < 100) Serial.print(" "); // pad tens
if ((torque[j * 8 + i]) < 10) Serial.print(" "); // pad ones
Serial.print(torque[j * 8 + i]); // data block
Serial.print(" "); // spacing
}
Serial.println(); // next line
counter = 0; // reset column counter
}
Serial.println(); // separate blocks
}
}
I2C é um protocolo serial. Sincrono, mas serial. O OP está correto, embora tenha sido genérico.
Salvo se tiver entendido muito errado a pergunta, a solução mais simples é o "método da força bruta":
bool teste_0 ;
bool teste_1 ;
bool teste_2 ;
bool teste_3 ;
bool teste_4 ;
bool teste_5 ;
bool teste_6 ;
bool teste_7 ;
byte teste ;
// Para ler as entradas digitais correspondentes as chaves, que imagino serem thumb wheels:
teste_0 = digitalRead( 2 ) ;
teste_1 = digitalRead( 3 ) ;
teste_2 = digitalRead( 4 ) ;
teste_3 = digitalRead( 5 ) ;
teste_4 = digitalRead( 6 ) ;
teste_5 = digitalRead( 7 ) ;
teste_6 = digitalRead( 8 ) ;
teste_7 = digitalRead( 9 ) ;
// Para reconstruir a variavel 'teste'
teste = ( (byte)teste_0 )
| ( (byte)(teste_1) << 1 )
| ( (byte)(teste_2) << 2 )
| ( (byte)(teste_3) << 3 )
| ( (byte)(teste_4) << 4 )
| ( (byte)(teste_5) << 5 )
| ( (byte)(teste_6) << 6 )
| ( (byte)(teste_7) << 7 ) ;
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.