does not name a type

someone help me debuggg this code

// libraries definition
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include “RTClib.h”

// frequency musical notes
#define NOTE_C6 1047
#define NOTE_C3 131
#define NOTE_G3 196

// pins definition
int levelSensorPin = 0;
int moistureSensorPin = 1;
int motionSensorPin=2;
int audioPin = 3;
int soggyLEDPin = 4;
int moistsoilLEDPin =5;
int drysoilLEDPin = 6;
int pumpLEDPin = 7;
int pumpPin = 8;
int motionSensorledPin = 13; // choose the pin for the LED
int pirState = LOW; // we start, assuming no motion detected // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

// variables
int levelSensorValue; // stores the level sensor values
int moistureSensorValue; // stores the moisture sensor values
int moistSensorValue; // stores the motion sensor values
int j = 0;

// system messages
const char *string_table =
{
" Welcome! =)",
" Tank LOW level",
" Dry soil",
" Moist soil",
" Soggy soil",
“The water pump is on”,
" ArduinoByMyself",
" Watering System",
" Please wait!"
};

// objects definition
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,20,4);

void setup(){
// serial initialization
Serial.begin(9600);

// LCD initialization
lcd.init();
lcd.backlight(); // with Backlight
lcd.clear(); // clearscreen

// Wire initialization
Wire.begin();

// RTC initialization
RTC.begin();
if (!RTC.isrunning()){
// date and time adjust as the PC computer date and time
RTC.adjust(DateTime(DATE, TIME));
}

// Arduino pins initalization
pinMode(audioPin, OUTPUT);
pinMode(soggyLEDPin, OUTPUT);
pinMode(moistsoilLEDPin,OUTPUT);
pinMode(drysoilLEDPin,OUTPUT);
pinMode(pumpLEDPin,OUTPUT);
pinMode(pumpPin,OUTPUT);
pinMode(motionSensorledPin, OUTPUT); // declare LED as output
pinMode(motionSensorPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);

// LCD initial messages
lcd.clear();
lcd.setCursor(0,0);
lcd.print(string_table[6]);
lcd.setCursor(0,1);
lcd.print(string_table[7]);
lcd.setCursor(0,3);
lcd.print(string_table[0]);
// initialization delay
delay(5000);
}

void loop(){
j= digitalRead(motionSensorPin); // read input value
if (pirState == HIGH) { // check if the input is HIGH
digitalWrite(motionSensorledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(150);

if (pirState == LOW) {
// we have just turned on
Serial.println(“Motion detected!”);
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(motionSensorledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println(“Motion ended!”);
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}

// RTC parameters definition
DateTime myRTC = RTC.now();
int H = myRTC.hour();
int M = myRTC.minute();
int S = myRTC.second();

// call Clock Function
//RightHour();

// reads the sensors
levelSensorValue = analogRead(levelSensorPin);
moistureSensorValue = analogRead(moistureSensorPin);
motionSensorValue = digitalRead(motionSensorPin);

// if low water level: plays the low level alarm
if(levelSensorValue > 600){
// system messages
lcd.clear();
RightHour();
lcd.setCursor(0,3);
lcd.print(string_table[1]);
// plays the alarm sound
for(int i=0;i<2;i++){
tone(audioPin, NOTE_G3, 200);
delay(200);
tone(audioPin, NOTE_C3, 200);
delay(200);
noTone(audioPin);
}
}

// check the moisture range
if(moistureSensorValue >= 700){
// in case of dry soil:
// system messages
lcd.clear();
RightHour();
lcd.setCursor(0,3);
lcd.print(string_table[2]);
// lights up the correct LED
digitalWrite(drysoilLEDPin,HIGH);
digitalWrite(moistsoilLEDPin,LOW);
digitalWrite(soggyLEDPin,LOW);
// plays the alarm sound
tone(audioPin, NOTE_C6, 100);
delay(250);
noTone(audioPin);
}
if((moistureSensorValue < 700) && (moistureSensorValue >= 300)){
// in case of moist soil:
// system messages
lcd.clear();
RightHour();
lcd.setCursor(0,3);
lcd.print(string_table[3]);
// lights up the correct LED
digitalWrite(drysoilLEDPin,LOW);
digitalWrite(moistsoilLEDPin,HIGH);
digitalWrite(soggyLEDPin,LOW);
delay(250);
}
if(moistureSensorValue < 300){
// in case of soggy soil:
// system messages
lcd.clear();
RightHour();
lcd.setCursor(0,3);
lcd.print(string_table[4]);
// lights up the correct LED
digitalWrite(drysoilLEDPin,LOW);
digitalWrite(moistsoilLEDPin,LOW);
digitalWrite(soggyLEDPin,HIGH);
delay(100);
}

// if the soil is dry and if it is the right time: turn on the pump for 1 minute
if((H == 16) && (M == 50) && (S == 00)){
while(moistureSensorValue >= 700){
// system messages
lcd.clear();
RightHour();
lcd.setCursor(0,1);
lcd.print(string_table[8]);
lcd.setCursor(0,3);
lcd.print(string_table[5]);
// turn the pump on
digitalWrite(pumpPin,HIGH);
digitalWrite(pumpLEDPin,HIGH);
delay(10000);
// if the soil is not moist so far
// reads the moisture sensor once more
moistureSensorValue = analogRead(moistureSensorPin);
}
// turn the pump off
digitalWrite(pumpPin,LOW);
digitalWrite(pumpLEDPin,LOW);
}

}

// Real Time Clock Function
void RightHour()
{
DateTime Now = RTC.now();
String clock_date = " Date: ";
String clock_hour = " Time: ";

int _day = Now.day();
int _month = Now.month();
int _year = Now.year();

clock_date += fixZero(_day);
clock_date += “/”;
clock_date += fixZero(_month);
clock_date += “/”;
clock_date += _year;

int _hour = Now.hour();
int _minute = Now.minute();
int _second = Now.second();

clock_hour += fixZero(_hour);
clock_hour += “:”;
clock_hour += fixZero(_minute);
clock_hour += “:”;
clock_hour += fixZero(_second);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(clock_date);
lcd.setCursor(0, 1);
lcd.print(clock_hour);

delay(500);
}

String fixZero(int i)
{
String ret;
if (i < 10) ret += “0”;
ret += i;
return ret;
}

these are the error message am getting

Arduino: 1.6.12 (Windows 10), Board: “Arduino/Genuino Uno”

MAKE_PIR_Sensor:147: error: ‘levelSensorValue’ does not name a type

levelSensorValue = analogRead(levelSensorPin);

^

MAKE_PIR_Sensor:148: error: ‘moistureSensorValue’ does not name a type

moistureSensorValue = analogRead(moistureSensorPin);

^

MAKE_PIR_Sensor:149: error: ‘motionSensorValue’ does not name a type

motionSensorValue = digitalRead(motionSensorPin);

^

MAKE_PIR_Sensor:152: error: expected unqualified-id before ‘if’

if(levelSensorValue > 600){

^

exit status 1
‘levelSensorValue’ does not name a type

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

When your code requires a library that’s not included with the Arduino IDE please always post a link (using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

From your indentation it looks like you intended to have everything from line 136 to line 234 of your sketch inside a function but it’s not. The easy way to catch things like this is to do a Tools > Auto Format and then examine the automatic indentation of the code to be sure the structure is as expected.

av tried the tools then autoformat but am still getting the error

Arduino: 1.6.12 (Windows 10), Board: "Arduino/Genuino Uno"

MAKE_PIR_Sensor:147: error: 'levelSensorValue' does not name a type

 levelSensorValue = analogRead(levelSensorPin);

 ^

MAKE_PIR_Sensor:148: error: 'moistureSensorValue' does not name a type

 moistureSensorValue = analogRead(moistureSensorPin);

 ^

MAKE_PIR_Sensor:149: error: 'motionSensorValue' does not name a type

 motionSensorValue = digitalRead(motionSensorPin);

 ^

MAKE_PIR_Sensor:152: error: expected unqualified-id before 'if'

 if (levelSensorValue > 600) {

 ^

exit status 1
'levelSensorValue' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You need to fix the actual problem. I suggested Auto Format only as a tool to help you locate bugs in your code when you encounter an error.

Please read what I said in my reply again:

pert:
From your indentation it looks like you intended to have everything from line 136 to line 234 of your sketch inside a function but it’s not.

That is the cause of the errors. You need to put that code in a function. Maybe it was intended to be part of loop() and you added an extra brace? Maybe it was intended to be a separate function and you accidentally left the start of the function off?

its my humble request if you are in a position help compile the code for me and send me a working code.
i have tried all i can but its proving to be a hard task .

Sorry, that’s not how I work. I’m happy to help you but I’m not going to write your code for you. You need to take the time to understand what every line of the code does, then the solution will be clear. I’ve already told you the cause of the errors and how to fix them.

After the autoformat, you need to look at your code to spot mismatches between { and }. Function definitions should start at the beginning of a line (yours do), anything that you expect to be inside a function should never start at the beginning of a line (e.g. an if()); yours don’t.

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq)
{
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration)
  {
    digitalWrite(pinSpeaker, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pinSpeaker, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
  }
}

...
...
// if low water level: plays the low level alarm
if (levelSensorValue > 600)
{

I suspect that that last } does not belong there or as pert indicated the beginning of a function is missing; later on you have two curly braces

...
...
  // turn the pump off
  digitalWrite(pumpPin, LOW);
  digitalWrite(pumpLEDPin, LOW);
}

}

// Real Time Clock Function
void RightHour()
{

If you want someone to code for you, Gigs and Collaborations is the forum, and be prepared to reimburse
people for their time and skill.

Here we help with troubleshooting problems and pointing people in the right direction by use of
experience (its easy and quick for us to catch problems, typically, and many eyes are available).
Its an efficient use of our good will basically!

The way to have someone write it for you here, is to play along and "just almost get it", but mysteriously backslide. Play one responder against another until it becomes a contest. Try to extend the thread to at least 50 replies. With this mixture of competition and frustration, someone will often get fed up and write and post the code. But you have to be patient. :slight_smile:

av tried introducing two distinct functions in the void loop area and later calling the functions but still the code not working

Not working or not compiling? For the first, describe what it does and what you want it to do. For the second, did you fix what I mentioned in reply #6 ?

the code is not compiling.

the code is meant to automate operations in the farm,am having a moisture sensor which has to sense the level of moisture in the soil and if it detects the level of moisture is low then it triggers the water pump to pump water to the garden.I
There is also water level sensor which detects if the water level in the storage tank is below the required level.
There is also the motion sensor which is supposed to detect intrusion in the farm.

i tried to fix what you mentioned in reply #6 but still the error kept manifesting

If you want help, post your code.

It's very simple.

Post the exact error message too.

Hi,
Have you developed your code in stages?

  • Write clock code and get it working on its own.
  • Write level sensor code and get it working on its own.
  • Write tone output code and get it working on its own.
  • Write LCD code and get it working on its own.
  • Write PIR code and get it working on its own.
  • Write Pump Motor control code and get it working
  • Combine clock and LCD code and get it working.
  • Combine level sensing code and get it working.
  • Combine pump motor control code and get it working.
  • Combine Alarm tone code and get it working.
  • Combine PIR code and get it working.

It sounds long and laborious but it will get you better code and be easier for you to debug.

Can you tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :slight_smile:

thanks alot guys who helped me to come up with the solution.
i can finally smile :slight_smile: :slight_smile: :slight_smile:

i have solved it....

Hi,
What did you do to fix it?

Tom.... :slight_smile: