Peristaltic Doser - Doser set amount over 24hr period

That is primary school maths.

If you have several crates of 100 apples each and have one partly filled crate with only 15 apples in it, but want to sell 20 apples to a customer, how would you do that?

The crates stand for your days and the apples are the seconds, just the numbers are a bit bigger and not as nice to look at :wink:

1 Like

I really do feel like an idiot here…but I appreciate the patience.

My answer would be you take apples from another crate.

Exactly that, but how would you write that down mathematically?

Your problem is

15 - 20 = negative, but that won't do

so we need to borrow a full crate

(15 + 100) - 20 = 95 apples left in the borrowed crate

And for seconds and days?
How many seconds of the previous day are left if you have it today 0:15 minus 1 hour?

Assuming 0:15 is minutes
(900 + 86400) - 3600 = 83700

Yup, that’s it.
(Actually 0:15 was meant as 12:15am, but still the same answer)

So if you check for end < start and then add 86400 if that’s the case, you have managed the rolling over day.
Another way would be to do the calculation first without the ā€œborrowingā€ and only if the result is negative ā€œnormalizeā€ the result by adding 86400.

There are multiple other solutions you should add to your mental repository in order to be able to tackle such problems.
e.g.
You need to see times and dates as one thing and not two seperate, as minutes and seconds are actually the same thing just in a different scale.
You need to understand the difference between signed and unsigned maths and what happens if you mix.
You might also want to look at the modulus operation (88200 % 86400 = 1800) as well as integer vs. floating point division.

These basics will help you a lot when dealing with numbers and range checks.

I actually read a bit about modulus, but didn’t want to get lost in another world for the moment. So now that I get the math, I’m trying to understand how this will fit into this code.

void loop() {
    now = Time.hour() * 3600 + Time.minute() * 60 + Time.second();
    Serial.println(now);
    for (int i=0;i<4;i++) {
        updateChannel(i);
    }
    
    
    delay(5000);
}



void updateChannel(int index) {
    Channel *c = &channels[index];
    if (c->onTime <= now && c->totalDose > 0) { 
        digitalWrite(c->connectedPin, HIGH);
        Serial.printlnf("Fired Relay %d", index);
        c->onTime += 3600;
    }

    if (c->offTime <= now && c->totalDose > 0) {
        digitalWrite(c->connectedPin, LOW);
        Serial.printlnf("Turned off Relay %d", index);
        c->offTime += 3600;
    }
}

Maybe I’m reading this wrong. But I see this as telling the functions to run every 3600 seconds. Do I need to tell photon earlier in the loop to compensate for the time or do I do it within each of these statements?

I was seeing something like this for the time check:

if (onTime > offTime)
offTime = offTime + 86400;

Any time greater than 86399 is never going to be <= to "now" right? So, that's not going to work. Your math needs to give an answer that's always less then 86400, or the pumps won't ever turn off.

I actually knew that, not sure why I put that down,

if (onTime > offTime)
offTime = offTime + 86399;

Am I on the right track here?

No, you’re not. If offTime is anything greater than 0 then offTime + 86399 is greater than 86399, right? Think about it this way,

11 PM is 82800 in the way we’re telling time. You want it to fire one hour later, so you add 3600,

82800 + 3600 = 86400 But midnight should be 0. What mathematical operation do you need to perform on 86400 to make it equal 0?

Either multiply by 0 or subtract 86400

Yeah… and which of those is the correct way to do it? Think about other cases.

Multiply for sure!!

Hey @james211!

Just wanted to pop in here to to comment that this sounds like an awesome product, and that I'm blown away by the time and help that @Ric, @ScruffR, @Moors7, and @peekay123 have contributed over the last couple of months.

I want to echo the sentiment that the forums are a really great place for getting tips, but isn't necessarily intended as a place to do continued product development. @Ric has been very generous with his time and expertise, which is fantastic, but if you're building a commercial product we highly recommend you also reach out to Particle.

We have a bunch of great resources specifically built to support product creators, including access to a network of paid contractors for developmental services (like you requested in your post at the beginning in the thread!).

4 Likes

I went ahead and changed the category of the thread to Product Creation to better fit the conversation occurring in the thread.

Sigh.... I give up.

3 Likes

Hi Will,

Yes, they have been incredibly generous. I have ideas and never know how to implement them. I just want to be clear that this is not a project I intend to profit from, its a project to help out a community of coral reef enthusiasts. I do a lot of work to help support others as well. I don’t expect anything from anyone and honestly I’m surprised they haven’t given up yet (but I know they are incredibly frustrated by me), I’m guessing I’m going to need to find additional resources soon.

I appreciate you chiming in, and I’ll check out the resources for paid contractors. I’m happy to pay. I’d rather pay than frustrate others…that is not my intention.

Thank you.
Dustin

I’ll let it be @Ric, life’s too short for me to create frustrations in the lives of others why I’m the one lacking knowledge. I appreciate your help and patience…I mean that sincerely, Thank you.

Hey @james211–I want to thank you for your enthusiasm! I don’t believe that you have offended or frustrated anyone. I would be very glad if all of our community members were as ambitious as you…many of the individuals on the Particle team learned about electronics and firmware development with the help of open source communities like this one, so I sincerely hope you continue your process of learning!

I would love to see you continue to post updates on the progress of your project! Best of luck!

2 Likes

Besides serial monitor, is there a way to see activity logs of the spark? Say when a relay is triggered on/off?

@james211, you could use Particle.publish() events to see those in the dashboard or using CLI.