Hi,
Iam studying in 6th class and iam using Arduino to make water harvesting project.
I coded Arduino I.D.E by watching basic tutorials. The coding was done for rain detector. Next stage I had to code water atm. I had only one board so I had to make Code Interrupts. I pieced together the codes, but when I verified it, it gave three errors:
1.35:29:Expected Initializer before 'wateratmcontrolInterrupt'.
2.In function 'void setup':22:13:Expected Initializer before '.' token.
3.At global scope:37:1:Expected Initializer before 'void'.
Here's the code: #include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
int digitalPin = 3;
int analogPin = A0;
int digitalVal ;
int analogVal ;
const int relayPin = 2;
void setup() {
pinMode(analogPin,INPUT);
tap_servo.attach(tap_servo_pin);
pinMode[digitalPin, INPUT];
pinMode[relayPin, OUTPUT];
int Serial.begin[100]
attachInterrupt [digitalPinToInterrupt [digitalPin] ,wateratmcontrolInterrupt,CHANGE] // put your setup code here, to run once:
}
void loop() {
val == digitalRead('sensor_Pin');
if (val == 0)
{tap_servo.write(0);
}
if (val == 1)
{tap_servo.write(90);
} // Put your first main code here, to run repeatedly:
}
volatile int digitalVal
void wateratmcontrolInterrupt() {
digitalVal == digitalRead[digitalPin]
if [digitalVal == LOW]
{
digitalWrite[relayPin,LOW]
}
else
{
digitalWrite[relayPin,HIGH]
}
analogVal = analogRead[analogPin];
Serial.print["Digital Output: "];
Serial.print[digitalVal];
Serial.print["Analog Output: "];
Serial.print[analogVal];
delay[100] // Put your second main code here, to run repeatedly:
}
As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum
Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Thank you for the help. I changed the code, but the same errors persist:
Errors are:
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:33:29: error: expected initializer before 'attachInterrupt'
}
^
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino: In function 'void setup()':
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:20:13: error: expected initializer before '.' token
int Serial.begin(100);
^
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino: At global scope:
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:36:1: error: expected initializer before 'void'
void attachInterrupt() {
^~~~
exit status 1
Compilation error: expected initializer before 'attachInterrupt'
Revised code below: #include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
int digitalPin = 3;
int analogPin = A0;
int digitalVal ;
int analogVal ;
const int relayPin = 2;
void setup() {
pinMode(analogPin,INPUT);
tap_servo.attach(tap_servo_pin);
pinMode(digitalPin, INPUT);
pinMode(relayPin, OUTPUT);
int Serial.begin(100);
attachInterrupt (digitalPinToInterrupt (digitalPin) , attachInterrupt, CHANGE); // put your setup code here, to run once:
}
void loop() {
val == digitalRead('sensor_Pin');
if (val == 0)
{tap_servo.write(0);
}
if (val == 1)
{tap_servo.write(90);
} // Put your first main code here, to run repeatedly:
}
volatile int digitalVal
void attachInterrupt() {
digitalVal == digitalRead(digitalPin)
if (digitalVal == LOW)
{
digitalWrite(relayPin,LOW)
}
else
{
digitalWrite(relayPin,HIGH)
}
analogVal = analogRead(analogPin);
Serial.print("Digital Output: ");
Serial.print(digitalVal);
Serial.print("Analog Output: ");
Serial.print(analogVal); // Put your second main code here, to run repeatedly:
}
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:33:29: error: expected initializer before 'attachInterrupt'
}
^
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino: In function 'void setup()':
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:20:13: error: expected initializer before '.' token
int Serial.begin(100);
^
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino: At global scope:
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:36:1: error: expected initializer before 'void'
void attachInterrupt() {
^~~~
exit status 1
Compilation error: expected initializer before 'attachInterrupt'
Code:
#include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
int digitalPin = 3;
int analogPin = A0;
int digitalVal ;
int analogVal ;
const int relayPin = 2;
void setup() {
pinMode(analogPin,INPUT);
tap_servo.attach(tap_servo_pin);
pinMode(digitalPin, INPUT);
pinMode(relayPin, OUTPUT);
int Serial.begin(100);
attachInterrupt (digitalPinToInterrupt (digitalPin) , attachInterrupt, CHANGE); // put your setup code here, to run once:
}
void loop() {
val == digitalRead('sensor_Pin');
if (val == 0)
{tap_servo.write(0);
}
if (val == 1)
{tap_servo.write(90);
} // Put your first main code here, to run repeatedly:
}
volatile int digitalVal
void attachInterrupt() {
digitalVal == digitalRead(digitalPin)
if (digitalVal == LOW)
{
digitalWrite(relayPin,LOW)
}
else
{
digitalWrite(relayPin,HIGH)
}
analogVal = analogRead(analogPin);
Serial.print("Digital Output: ");
Serial.print(digitalVal);
Serial.print("Analog Output: ");
Serial.print(analogVal); // Put your second main code here, to run repeatedly:
}
Please correct your post and add code tags around your code.
There is a small pencil below your existing posts.
click on this pencil ➜ that will let you edit your post.
Select the part of the text that corresponds to the code
Click on the <code/> icon in the toolbar to indicate that it is code
click Save Edit
(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)
#include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
int digitalPin = 3;
int analogPin = A0;
'int digitalVal'x616c;
int analogVal ;
const int relayPin = 2;
void setup() {
pinMode(analogPin,INPUT);
tap_servo.attach(tap_servo_pin);
pinMode(digitalPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(100);
attachInterrupt (digitalPinToInterrupt (digitalPin) , attachInterrupt, CHANGE); // put your setup code here, to run once:
}
void loop() {
val == digitalRead('sensor_Pin');
if (val == 0)
{tap_servo.write(0);
}
if (val == 1)
{tap_servo.write(90);
} // Put your first main code here, to run repeatedly:
}
volatile int digitalVal;
void attachInterrupt() {
digitalVal == digitalRead(digitalPin);
if (digitalVal == LOW)
digitalWrite(relayPin,LOW);
}
else
digitalWrite(relayPin,HIGH);
}
analogVal = analogRead(analogPin);
Serial.print("Digital Output: ");
Serial.print(digitalVal);
Serial.print("Analog Output: ");
Serial.print(analogVal); // Put your second main code here, to run repeatedly:
}
Errors:
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:11:1: error: expected unqualified-id before user-defined character literal
'int digitalVal'x616c;
^~~~~~~~~~~~~~~~~~~~~
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:41:3: error: expected unqualified-id before 'else'
else
^~~~
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:43:3: error: expected declaration before '}' token
}
^
exit status 1
Compilation error: expected unqualified-id before user-defined character literal
I actually posted it before your first post, but here is the updated one.
Code:
#include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
int digitalPin = 3;
int analogPin = A0;
'int digitalVal'x616c;
int analogVal ;
const int relayPin = 2;
void setup() {
pinMode(analogPin,INPUT);
tap_servo.attach(tap_servo_pin);
pinMode(digitalPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(100);
attachInterrupt (digitalPinToInterrupt (digitalPin) , attachInterrupt, CHANGE); // put your setup code here, to run once:
}
void loop() {
val == digitalRead('sensor_Pin');
if (val == 0)
{tap_servo.write(0);
}
if (val == 1)
{tap_servo.write(90);
} // Put your first main code here, to run repeatedly:
}
volatile int digitalVal;
void attachInterrupt() {
digitalVal == digitalRead(digitalPin);
if (digitalVal == LOW)
digitalWrite(relayPin,LOW);
}
else
digitalWrite(relayPin,HIGH);
}
analogVal = analogRead(analogPin);
Serial.print("Digital Output: ");
Serial.print(digitalVal);
Serial.print("Analog Output: ");
Serial.print(analogVal); // Put your second main code here, to run repeatedly:
}
Errors:
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:11:1: error: expected unqualified-id before user-defined character literal
'int digitalVal'x616c;
^~~~~~~~~~~~~~~~~~~~~
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:41:3: error: expected unqualified-id before 'else'
else
^~~~
C:\Users\DELL\Documents\Arduino\Rain_detector\Rain_detector.ino:43:3: error: expected declaration before '}' token
}
^
exit status 1
Compilation error: expected unqualified-id before user-defined character literal
It actually used to be only int digitalVal but the program suggested to change it.
It apparently means the Digital Value of the Magnetic Sensor to the Water Pump.
Just wrong. Interrupts are probably not gonna be your friend here. In particular reading a pushbutton with an interruot is not only a bad idea, but it is kinda hard to do correctly even if you insisted…
You had an earlier attachInterruot() call that looked OK.