I need same sensor data speed on second core. I’m having trouble with sending ping sensor on second core.
Core #1
//Ping
const int pingPin = D7;
unsigned int duration, inches;
void setup() {
Serial.begin(115200);
}
void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
Serial.println(inches); // Display result
///// I need help here.
Spark.publish("Ping",string(inches), 1, PRIVATE);
}
Core #2
//CLI Terminal. $ particle serial monitor
void setup() {
Spark.subscribe("Ping", eventHandler, MY_DEVICES);
Serial.begin(115200);
}
void loop() {
}
void eventHandler(const char *event, const char *data)
{
Serial.print(data);
}
With Particle.publish()
you have a rate limit of one per second (with a burst of four).
If you violate this limit you need to wait at least one second after your last attempt before it’ll work again.
As I hand wave ping sensor. I got strange data from core 1. Not the same.
It’s stops.
I want to do something like this.
Since your description of the problem is still a bit nebulous for me and I don’t own one of these sensors, I’ll just try a stab in the dark.
uint32_t msLastPublished;
void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
pinResetFast(pingPin); // Ensure pin is low (one of the fastes ways)
delayMicroseconds(2);
pinSetFast(pingPin); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
pinResetFast(pingPin); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 148.0; // Convert to inches
Serial.println(inches); // Display result
if (millis() - msLastPublished > 1000) // obey rate limit one per sec
{
Serial.print("Publishing "); // Signal publishing
Serial.println(inches);
Particle.publish("Ping", String(inches), 1, PRIVATE); // you shoule use capital S in String here
msLastPublished = millis();
}
delay(250);
}
So there I’ve added some rate limiting, use one of the fastes ways to set a specific pin HIGH/LOW, changed from string()
to String()
and simplified the conversion also.
By performing two integer division you each time would loose some precision, so do only one.
You also only talked about Core#2 receiving odd readings, what about the Serial.println()
of Core#1?
@ScruffR, I believe inches = duration / 74 / 2;
converts to inches = duration / 148.0;
.
It is important to note that the during the 1000ms publishing delay, the ping code will continue to run. There is an opportunity to calculate a running average during the delay, for example. 
1 Like
@peekay123: You were saying??? Who has ever said something else
?
Good catch, I should think twice, before coding.
I also thought about moving avg, but wanted to keep it as simple as possible (at first), but it’s a good suggestion for the final code.
1 Like