I am doing a project to control two sensors (ultrasonic and infrared), control them using Arduino. The IR receiver has a filter system inside, so it receives at 36 kHz. I use the srf04 module to process ultrasonic material. If I make a program that needs to control only one sensor, it works. But I have to interpolate two signals into one result. So I used proto-flows! But this does not work ... What kind of mistake?
Here is the code:
#include <pt.h> int iro = 8, iri = 4, us = 12, distanza, us_vcc = 13, ir_vcc = 7; long durata; static struct pt pt1, pt2, pt3; static int irthread(struct pt *pt) { PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, 1>0); digitalWrite(iro, HIGH); delayMicroseconds(9); digitalWrite(iro, LOW); delayMicroseconds(9); } PT_END(pt); } static int usthread(struct pt *pt) { static unsigned long timer = 0; PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, millis() - timer > 200); timer = millis(); pinMode(us, OUTPUT); digitalWrite(us, LOW); delayMicroseconds(5); digitalWrite(us, HIGH); delayMicroseconds(10); digitalWrite(us, LOW); pinMode(us, INPUT); durata = pulseIn(us, HIGH); distanza = durata/58; } PT_END(pt); } static int leggithread(struct pt *pt) { static unsigned long timer = 0; PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, millis() - timer > 200); timer = millis(); Serial.print(distanza); Serial.print("cm "); if (digitalRead(iri) == LOW) Serial.println("ir si"); else Serial.println("ir no"); } PT_END(pt); } void setup() { pinMode(iro, OUTPUT); pinMode(iri, INPUT); pinMode(us_vcc, OUTPUT); digitalWrite(us_vcc, HIGH); pinMode(ir_vcc, OUTPUT); digitalWrite(ir_vcc, HIGH); Serial.begin(9600); PT_INIT(&pt1); PT_INIT(&pt2); PT_INIT(&pt3); } void loop() { irthread(&pt1); usthread(&pt2); leggithread(&pt3); }
Separate pieces of code for each thread work.
Refresh
I solved my problem (excluded irthread() ), and the code now looks like this:
#include <pt.h> int iro = 8, iri = 4, us = 12, distanza, us_vcc = 13, ir_vcc = 7; long durata; static struct pt pt1, pt2; static int usthread(struct pt *pt) { static unsigned long timer = 0; PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, millis() - timer > 200); timer = millis(); pinMode(us, OUTPUT); digitalWrite(us, LOW); delayMicroseconds(5); digitalWrite(us, HIGH); delayMicroseconds(10); digitalWrite(us, LOW); pinMode(us, INPUT); durata = pulseIn(us, HIGH); } PT_END(pt); } static int leggithread(struct pt *pt) { static unsigned long timer = 0; PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, millis() - timer > 200); timer = millis(); distanza = durata/58; Serial.print(distanza); Serial.print("cm "); if(digitalRead(iri) == LOW) Serial.println("ir si"); else Serial.println("ir no"); } PT_END(pt); } void setup() { pinMode(iro, OUTPUT); tone(iro, 36000); pinMode(iri, INPUT); pinMode(us_vcc, OUTPUT); digitalWrite(us_vcc, HIGH); pinMode(ir_vcc, OUTPUT); digitalWrite(ir_vcc, HIGH); Serial.begin(9600); PT_INIT(&pt1); PT_INIT(&pt2); } void loop() { usthread(&pt1); leggithread(&pt2); }
Now the problem is in the ultrasonic sensor. If I manage it in one program without protoflows, it can reach objects at a distance of up to 3 meters. Now, even if I put something on 1 meter, the "distance" is a maximum of 15 cm. What is the mistake?