BUG no FOR

Projetos de ESP32
dandrade
Membro
Mensagens: 9
Registrado em: 27/Ago/2020, 13:45

BUG no FOR

Mensagem por dandrade » 27/Ago/2020, 14:14

Saudações

Preciso de ajuda para resolver esse BUG.
Estou utilizando a seguinte plaquinha https://www.olimex.com/Products/IoT/ESP ... e-hardware
Ocorre, algo muito estranho, com a variável dutyCycle do tipo byte , que apresenta valores maiores que 255.

Código: Selecionar todos


// include the library code:
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <analogWrite.h> //Instalar biblioteca ESP32 AnalogWrite

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 04,  rw = 05, en = 13, d4 = 14, d5 = 15, d6 = 16, d7 = 32  ;
LiquidCrystal lcd(rs, rw, en, d4, d5, d6, d7);


const int led=1;
const int brilho=2;
const int freq = 500;
const int ledChannel = 0;
const int brilhoChannel = 1;
const int resolution = 8;
int val=0;
void setup() {
  
  Serial.begin(9600); 
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(led, ledChannel);

  ledcAttachPin(brilho, brilhoChannel);

  delay(100);
  // pinMode(led, OUTPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);

  lcd.clear();
  // Turn on the display:
  //lcd.display();
  delay(100);
  // Print a message to the LCD.
  
   lcd.setCursor(0, 0);
  lcd.print("hello, world!");
 
}

void loop() {

// For abaixo estoura mostrando valores acima de 255. Segue ate 999 e reinicia em 100. Segue pulando em centenas e depois dezenas	
   for( byte dutyCycle = 0; dutyCycle <= 255;  dutyCycle++){  
        
    // changing the LED brightness with PWM
    //ledcWrite(ledChannel, dutyCycle); //contraste
    ledcWrite(ledChannel, 200); //contraste fixo
    ledcWrite(brilhoChannel, dutyCycle); //brilho
    delay(150);
    //  digitalWrite(led, 1);
    lcd.setCursor(0, 0);
     lcd.print("BEM VINDO AO world!");
     lcd.setCursor(0, 1);
     lcd.print("987654321");
     lcd.setCursor(0, 2);
     lcd.print("1234567890");
     lcd.setCursor(14, 2);     
     lcd.print(dutyCycle);
    Serial.print("Duty "); 
    Serial.print(dutyCycle);   
  
    }//for
 
}

Anexos
LCD_20colunas_4linhas.ino.esp32-poe.zip
Arquivo binário
(107.7 KiB) Baixado 336 vezes

dandrade
Membro
Mensagens: 9
Registrado em: 27/Ago/2020, 13:45

Re: BUG no FOR

Mensagem por dandrade » 04/Set/2020, 13:02

Resolvido, o erro era na escrita do dado no LCD, sobrepondo as unidades,dezenas e centenas, cujo impressão desses valores gerava uma visualização errada dos valores.

Fiz uma inserção de caracteres vazios para apagar a linha e em seguida utilizei a função lcd.rightToLeft() , para escrever da direita para esquerda a variável dutyCycle

Responder