i want to combine them in one sketch

Hi all,

this is my first time at working with the Arduino and im loving it.

Im just having trouble at trying to run several things at the same time.

I have two sketches, sinewave aand trianglewave progams and i want to combine them in one sketch. so i can choose one of them for output.

That’s OK - you don’t need to ask permission.

Several things at the same time:https://forum.arduino.cc/index.php?topic=223286.0

holdingpattern:
In general, stick the code in 2x functions say doSine() and do(Triangle), and call either of them from loop() according to a switch?

That's easy. Code it and come back if you have any problems. It's only when you have to make many things happen at the same time, that you need the approach that I linked to.

I would use more descriptive label than doSomething()... like displaySine or showSine etc...

If you want to display a triangle and a sine at the same time, that is a completely different story.

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);
}
}
}