Unexpected code error

Hi, I was just writing some code and an unexpected error occurred here:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 


DHT dht(DHTPIN, DHTTYPE);
int humi = 0;
int input = 15;
int output = 14;
void setup() {
    lcd.begin(16, 2);
    dht.begin();
    lcd.noDisplay();
    delay(100);
    lcd.display();
    lcd.write("Greenhouse system");
    delay(3000);
    lcd.clear();
    delay(30);
    lcd.write("Loading info ...");
    delay(3000);
    lcd.clear();
    delay(100);
}

void loop(){
  delay(2000);
     float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
   lcd.setCursor(1, 1);
   lcd.write("Temp(f)= ");
   lcd.setCursor(10, 1);
   lcd.write(dht.readTemperature());
   lcd.setCursor(1, 2);
   lcd.write("Humidity = ");
   lcd.setCursor(12, 2);
   lcd.write(dht.readHumidity());
   humi = analogRead(input);
   output = analogWrite(humi);
}

Then the compiler says this:

/tmp/802097090/sketch_jul24a/sketch_jul24a.ino: In function 'void loop()':

/tmp/802097090/sketch_jul24a/sketch_jul24a.ino:51:29: error: too few arguments to function 'void analogWrite(uint8_t, int)'

output = analogWrite(humi);

^

In file included from /tmp/802097090/build/sketch/sketch_jul24a.ino.cpp:1:0:

/home/builder/.arduino15/packages/arduino/hardware/avr/1.8.3/cores/arduino/Arduino.h:139:6: note: declared here

void analogWrite(uint8_t pin, int val);

^~~~~~~~~~~

exit status 1

What's unexpected about an error concerning assigning a value that a function doesn't return?

It means exactly what it says - too few arguments. analogWrite() takes 2 arguments - a pin, a number between 0 and 255 representing the desired duty cycle?

Also, you're setting a variable equal to the value returned by analogWrite(). Which returns nothing

output = analogWrite(humi);

analogWrite takes 2 arguments, pin number and value and does not return a value. Did you perhaps mean to use analogRead() ?

Its weird because this error has not happened before.

Maybe you have never made that mistake before

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to

Pin 2 should not be connected to both the lcd and the dht sensor.

I learned long ago to expect "unexpected errors" when I write code.

I also learned that the compiler and linker error messages usually give helpful hints about how to solve those unexpected problems.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.