I tried combining these two sketches but the two sensors are competing with each other. How can I solve this problem?
This is the code for the smoke sensor:
#include <TFT.h>
int smokeA0 = A0;
int sensorThres = 200;
void setup () {
pinMode (smokeA0, INPUT);
pinMode (13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int analogSensor = analogRead(smokeA0);
Serial.print("PinA0: ");
Serial.println(analogSensor);
if (analogSensor > sensorThres)
{
digitalWrite(13,HIGH);
delay(50);
digitalWrite(13,LOW);
delay(50);;
tone(8,1000,1000);
}
}
This is the code for the Human Touch Sensor with servo motor
const int servoPin = 7;
const int touchPin = 10;
int angle =80;
int angleStep =10;
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(7);
pinMode(servoPin, OUTPUT);
pinMode(touchPin, INPUT);
Serial.println();
}
void loop() {
while(digitalRead(2) == LOW){
int touchState = digitalRead(touchPin);
if (touchState == HIGH) {
angle = angle + angleStep;
if (angle <= 0 || angle >= 180) {
angleStep = -angleStep;
}
myservo.write(angle);
Serial.print(“Moved to: “);
Serial.print(angle);
Serial.println(” degree”);
delay(10);
}
}
}
And this is what I come up when I combined this two sketches:
#include <TFT.h>
int smokeA0 = A0;
int sensorThres = 200;
const int servoPin = 7;
const int touchPin = 10;
int angle =80;
int angleStep =10;
#include <Servo.h>
Servo myservo;
void setup () {
pinMode (smokeA0, INPUT);
pinMode (13, OUTPUT);
Serial.begin(9600);
{
myservo.attach(7);
pinMode(servoPin, OUTPUT);
pinMode(touchPin, INPUT);
Serial.println();
}
}
void loop() {
int analogSensor = analogRead(smokeA0);
Serial.print("PinA0: ");
Serial.println(analogSensor);
if (analogSensor > sensorThres)
{
digitalWrite(13,HIGH);
delay(50);
digitalWrite(13,LOW);
delay(50);;
tone(8,1000,1000);
}
{
int touchState = digitalRead(touchPin);
if (touchState == HIGH) {
angle = angle + angleStep;
if (angle <= 0 || angle >= 180) {
angleStep = -angleStep;
}
myservo.write(angle);
Serial.print(“Moved to: “);
Serial.print(angle);
Serial.println(” degree”);
delay(10);
}
}
}