vendredi 17 août 2018

Arduino Episode 4 : LCD + 433MHz




(Arduino Nano)
Au même titre que mon raspberry, j'ai voulu utiliser mon Arduino pour afficher des informations sur mon écran LCD:

1°) Branchement :
GND sur GND
VCC sur 5V
SDA sur A4
SCL sur A5

Branchement du module 433Mhz
VCC sur 5V
GND sur GND
DATA sur D2

1BIS°) Interessant:
Il existe une fonction sur pour allumer l'écran:

lcd.backlight();
et une pour le mettre en veille.
lcd.noBacklight();

2°) Librairie sur l'outil ARDUINO IDE
Croquis / Inclure une Bibliothèque / Ajouter la bibliothèque ZIP :
http://downloads.arduino.cc/libraries/github.com/marcoschwartz/LiquidCrystal_I2C-1.1.2.zip

3°) Inclure et téléverser le code :

#include <liquidcrystal_i2c.h>


RCSwitch mySwitch = RCSwitch();
LiquidCrystal_I2C lcd(0x27, 20, 4);



void setup() {
lcd.init();
}

void loop() {
lcd.backlight();

lcd.setCursor(0,0);
lcd.print(" Test Ecran LCD");

lcd.setCursor(0,1);
lcd.print(" sur");

lcd.setCursor(0,2);
lcd.print("Arduino");

lcd.setCursor(0,3);
lcd.print("Nano");
}




Et si on rajoute un module 433 voici le code

#include <LiquidCrystal_I2C.h>
#include <RCSwitch.h>
#include <Wire.h>


RCSwitch mySwitch = RCSwitch();
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
lcd.init();
}

void loop() {
lcd.backlight();
if (mySwitch.available()) {
output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());


lcd.setCursor(0,0);
lcd.print("value: ");
lcd.setCursor(7,0);
lcd.print(mySwitch.getReceivedValue());

lcd.setCursor(0,1);
lcd.print("delai: ");
lcd.setCursor(7,1);
lcd.print(mySwitch.getReceivedDelay());

lcd.setCursor(0,2);
lcd.print("longu:");
lcd.setCursor(7,2);
lcd.print(mySwitch.getReceivedBitlength());

lcd.setCursor(0,3);
lcd.print("Proto: ");
lcd.setCursor(7,3);
lcd.print(mySwitch.getReceivedProtocol());
mySwitch.resetAvailable();
}
}


et la fonction OutPut, comprise dans les exemples

static const char* bin2tristate(const char* bin);
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);

void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {

if (decimal == 0) {
Serial.print("Unknown encoding.");
} else {
const char* b = dec2binWzerofill(decimal, length);
Serial.print("Decimal: ");
Serial.print(decimal);
Serial.print(" (");
Serial.print( length );
Serial.print("Bit) Binary: ");
Serial.print( b );
Serial.print(" Tri-State: ");
Serial.print( bin2tristate( b) );
Serial.print(" PulseLength: ");
Serial.print(delay);
Serial.print(" microseconds");
Serial.print(" Protocol: ");
Serial.println(protocol);
}

Serial.print("Raw data: ");
for (unsigned int i=0; i<= length*2; i++) {
Serial.print(raw[i]);
Serial.print(",");
}
Serial.println();
Serial.println();
}

static const char* bin2tristate(const char* bin) {
static char returnValue[50];
int pos = 0;
int pos2 = 0;
while (bin[pos]!='\0' && bin[pos+1]!='\0') {
if (bin[pos]=='0' && bin[pos+1]=='0') {
returnValue[pos2] = '0';
} else if (bin[pos]=='1' && bin[pos+1]=='1') {
returnValue[pos2] = '1';
} else if (bin[pos]=='0' && bin[pos+1]=='1') {
returnValue[pos2] = 'F';
} else {
return "not applicable";
}
pos = pos+2;
pos2++;
}
returnValue[pos2] = '\0';
return returnValue;
}

static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
static char bin[64];
unsigned int i=0;

while (Dec > 0) {
bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
Dec = Dec >> 1;
}

for (unsigned int j = 0; j< bitLength; j++) {
if (j >= bitLength - i) {
bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
} else {
bin[j] = '0';
}
}
bin[bitLength] = '\0';

return bin;
}

Aucun commentaire:

Enregistrer un commentaire