Hi to everyone!
I am working on a project using ESP32 and Dwin 7" display. I am using PWM pin to generate pulse with fix duty cycle and variable frequency. My program is generating pulses when I put an integer for frequency in line no.37 i.e. const int freq = 120; // set the frequency
. Here I get frequency 120 at PWM pin.
On sending any number from Dwin display (on Rx2) by pressing a button , number is received and printed in serial monitor. Please see line no.21 i.e. Serial.println(" "+ String(lastByte, DEC)+ " "); //Last byte of string in decimal
.
Now I want to set frequency in line no.37 according to the number received from Dwin display. But on putting received number in line no.37 ( const int freq = "+ String(lastByte, DEC)+ "; // set the frequency
), I am getting error as "Compilation error: invalid conversion from 'const char*' to 'int' [-fpermissive]".
Please help to eliminate the error so as to set the frequency of PWM pin according to the number received from the display.
I would be very thankful.
#include <Arduino.h>
#include <DWIN.h>
#define ADDRESS_A "1010"
#define ADDRESS_B "1020"
#include <Mapf.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;
#define DGUS_BAUD 115200
#define DGUS_SERIAL Serial2
DWIN hmi(DGUS_SERIAL, 16, 17, DGUS_BAUD);
/* For Software serial */
//#include <SoftwareSerial.h>
//const byte rxPin = 16; //rx2
//const byte txPin = 17; //tx2
//SoftwareSerial mySerial (rxPin, txPin);
// Event Occurs when response comes from HMI
void onHMIEvent(String address, int lastByte, String message, String response){
Serial.println(" "+ String(lastByte, DEC)+ " "); //Last byte of string in decimal
if (address == "1002"){
// Take your custom action call
}
}
float ldrValue;
//#define eNO_PIN 39 // VN pin connected with eNO
/* Adresses of all sensors */
unsigned char Buffer[9];
#define ldr_add 0x63
unsigned char eNO[8] = {0x5a, 0xa5, 0x05, 0x82, ldr_add , 0x00, 0x00, 0x00};
//PWM Code Part
const int ledPin = 13; // the PWM pin the LED is attached to
const int freq = 120; // set the frequency
const int ledChannel = 0; // set the PWM channel
const int resolution = 8; // set PWM resolution
void setup() {
ledcSetup(ledChannel, freq, resolution); // define the PWM Setup
ledcAttachPin(ledPin, ledChannel);
Serial.begin(115200);
//mySerial.begin(115200);
delay(100);
Serial.println("DWIN HMI ~ Hello World");
hmi.echoEnabled(false);
hmi.hmiCallBack(onHMIEvent);
hmi.setPage(1);
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
if (!ads.begin())
{
Serial.println("Failed to initialize ADS.");
while (1);
}
}
void loop() {
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);
volts0 = ads.computeVolts(adc0);
volts1 = ads.computeVolts(adc1);
volts2 = ads.computeVolts(adc2);
volts3 = ads.computeVolts(adc3);
Serial.println("-----------------------------------------------------------");
//Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");
Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V");
//Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V");
//Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" "); Serial.print(volts3); Serial.println("V");
//ldrValue = (volts1 - 0.4) * (156);
ldrValue = mapf(volts1, .4, 2, 0, 250);
Serial.print("eNO Value: ");
Serial.println(ldrValue);
Serial.println(freq);
//void Data_Arduino_to_Display() {
delay(1000);
int ldrVal = ldrValue;
/*------Send Data to Display------*/
eNO[6] = highByte(ldrVal);
eNO[7] = lowByte(ldrVal);
DGUS_SERIAL.write(eNO, 8);
//mySerial.write(eNO, 8);
//PWM Code Part
ledcWrite(ledChannel,10); // set the Duty cycle to 50 out of 255
// Listen HMI Events
hmi.listen();
}