Hi All
I would like some help if you don't mind. I am new to programming (of any kind). I have been through the basic projects and have successfully written code for things like blinking LEDS and the like.
Now I have ran into my first problem.
I have two sensors - 1. Ultrasonic 2. Temp
I want to take a reading from sensor 1 every 500 ms, and a reading from sensor 2 every 10000 ms.
Its not the code for the individual processes I have an issue with but I cant get them to work together.
Every way I have tried to compile this results in the ultrasonic sensor waiting for the temp sensor to take reading before it will take it second reading.
I understand that arduino cant run multiple simultaneous processes so does this mean i would need to use a separate arduino or is there a way to achieve this
I will include my code because that seems to be the done thing but really I am looking for general information as more sensors will be added later and all will have different timings.
End result wanted is a sensor bot, but at the moment i'm just monitoring readings through serial.
Code for Ultrasonic sensor:
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}Code for Temperature sensor:
float temp;
int tempPin1 = 0;void setup()
{
Serial.begin(9600);
}void loop()
{
temp = analogRead(tempPin1);
temp = temp * 0.48828125;
Serial.print("TEMPRATURE 1 = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(10000);
}Thanks in advance for any help you can give