@Alesqosim wrote:
Dear!
I am studying Arduino and is still a beginner. Please help in the development. rew the scheme here using the Fritzing program.
I th
Fritzing itself issues the firmware code after the reset is completed (as I understand it)
That’s all it takes to verify the correctness of this code.
Could you verify the correctness of such code?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27, 16, 2); // adress and format LCD
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached toint sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)void setup() {
lcd.init(); // init LCD
lcd.backlight();
lcd.home();
lcd.setCursor(5, 0); // place static text
lcd.print("ADC = ");
lcd.setCursor(15, 0);
lcd.print(“V”);
lcd.setCursor(0, 1);
lcd.print(“LED : “);
lcd.setCursor(12, 1);
lcd.print(”%”);
Serial.begin(9600); // optional serial monitor
}void loop() {
int sensorValue = analogRead(analogInPin); // read analog A0 en store in sensorValue
float voltValue = 5.0 / 1024.0 * sensorValue; // calculate volt at analog A0 and store in voltValue
float percentValue = sensorValue / 1024.0 * 100.0; // calculate percentage analog A0 and store in percentValue
outputValue = map(sensorValue, 0, 1023, 0, 255); // map analog input to analog output and store in outputValue
analogWrite(analogOutPin, outputValue); // write outputValue to analogOutPinlcd.setCursor(0, 0); // set cursor at deserid position
leadingZeros(sensorValue, 4); // print sensorvalue with number of leading-characters (0 or space as specified in function leadingZeros)
lcd.setCursor(10, 0); // set cursor at deserid position
lcd.print(voltValue); // print voltValue
lcd.setCursor(6, 1); // set cursor at deserid position
lcd.print(percentValue); // print percentValueSerial.print(“sensor = " ); // print the results to the serial monitor:
Serial.print(sensorValue);
Serial.print(”\t output = “);
Serial.print(outputValue);
Serial.print(”\t procent = ");
Serial.println(percentValue);delay(1000); // set delay
}void leadingZeros( int value, int width) { // display result with leading characters
char valueStr[6]; // large enough to hold an int
itoa (value, valueStr, 10); // itoa converts int to string
int len = strlen(valueStr); // calculate length of valueStr
if (len < width) {
len = width - len;
while (len–)
lcd.print(’ '); // for leading zeros place ‘0’ between (), for leading spaces place ’ ’ between ()
}
lcd.print(valueStr);
}
Posts: 2
Participants: 2