Pls help. Unable to club two different codes in one

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:
}

Kindly help. Thank you.

Welcome to the forum

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.

volatile int digitalVal

Missing semicolon

attachInterrupt [digitalPinToInterrupt [digitalPin] ,wateratmcontrolInterrupt,CHANGE] // put your setup code here, to run once:

Square brackets used instead of round brackets

if [digitalVal == LOW]

As above

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:
}

But you still did not use code tags when posting it

1 Like

what are code tags

volatile int digitalVal

No change

int Serial.begin(100);

What is that int doing there ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

If you follow the advice in my earlier reply you don't even need to know what they are

1 Like

Here it is now with code tags.

Errors:

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:
}

can't you make the effort to post with code tags ! that's not rocket science.
(tanks for correcting the last one, edit the faulty posts too please)

sorry for taking your time. Iam 10 years old and am trying to learn. now posting with code tags.

You got there

Now take note of reply #8

OK great.

Please correct your post and add code tags around your code.

There is a small pencil image 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 image 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)

I did everything but still there are errors.

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

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

you can still edit... please do so...

why do you have a ' before int ?

I meant one with code tags.
The program suggested to do so.

What is this intended to do ?

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.

attachInterrupt (digitalPinToInterrupt (digitalPin) , attachInterrupt, CHANGE);

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.

a7