Noob: howto count responses from app?

Hi,
Im really new at this so bear with me. I made my first Blynk app, the turn LED on/off.
it only has blynk.begin() and blynk.run() and it works
No w i want to be able to count how many times the button has been used (the light has been turned on).
Now how would i get this in my Arduino sketch? i don’t see how to get an ‘event’ or such form blynk.run().
tnx
RDG

1 Like

You have to set a counter and have it count each time the button/pin is HIGH and/or LOW. May I inquire as what you are trying to accomplish? Just curious :slight_smile:

I’m setting my first steps into the IoT playground:) the whole project is much bigger (i’m trying to make a remote controlled chicken feeder so my parents can stay a little longer with the kids when they are here, so they don’t have to go home to feed the chickens).
Anyway, my question is how would i write that counter in my sketch? how can i ‘read’ what has been send from the app to the arduino? my sketch is real skeleton and i don’t yet know how to get more data from Blynk?
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, “mywifi”, “mypassword”, WLAN_SEC_WPA2);
}

   void loop()
   {
    Blynk.run();
}

tnx
RDG

This is nice, a colleague of mine is doing something similar :wink:

Anyway, I can see why you probably want to count stuff, but are you sure that is the best solution? There are probably ways to achieve this without relying on counters.

int counter = 0; // Set counter to 0

// This here activates a button attached to Virtual Pin 1 on your dashboard
BLYNK_WRITE(V0)
{
  // Insert stuff here to have them feed the chickens, motor, relay, whatever you want
  
  // This will increase the counter with 1 step, you can use it anywhere throughout your program because it's declared Global (outside of a function, wether it be setup, loop or something you made)
  count++;
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "mywifi", "mypassword", WLAN_SEC_WPA2); 
}

void loop()
{
   Blynk.run();
}

Note however, that this value will reset if you reset your Arduino :slight_smile: If you want a more permanent storage, you should look at EEPROM writing.

@Lefthandmedia chicken feeder project sounds interesting and we look forward to hearing of your progress in due course.

I don’t know anything about chickens other than they are for sale at the local KFC and I know you are knew to Blynk but as @Lichtsignaal stated is counting really want you need?

Maybe you need to work through the basics of counting and the like but I would think that something like the timer widget will be the way to go. You can set the timer ‘to do things’ at certain times of the day i.e. feeding time. Or maybe chickens feed all day and you need to count how many times they have accessed the feeder etc.

Perhaps for us that know nothing about chickens you could indicate how they get fed and we can suggest a possible route for you to take.

3 Likes

:joy:

I think it should be

BLYNK_WRITE(V0)
{
    if (param.asInt()) {  
        count++;
    }
}
1 Like

Depends if you want to count only the number of ON’s or OFF’s or both :smile:

And now I’m hungry, thank you @Costas

1 Like

Hi,

So the BLYNK_WRITE() method does not have to be in the loop?

The whole project is for me and my daugther (14) to learn about IoT. I am a webdeveloper myself so i know about coding. It’s the hardware that fascinates and puzzles :smile:
I need the counter to check how many revolutions the wormwheel made. but more because i want to learn the principles. I’m so new at this that it is even difficult to formulate the right question. so thanks for the patience.
@Lichtsignaal: any chance i can see what/how your colleague is building?

I am searching for some good ways to learn in practice here in the Netherlands so if anyone knows about some good 101-level courses or workshops i’d be grateful
Tnx
RDG

Depends on where you live (I’m, as you might have guessed) from there too. I also have a background in web developing and programming but I managed to start really quick with Arduino’s and the whole Blynk stuff. If you are not too far away I guess we can arrange something :slight_smile:

I think my colleague hasn’t got any weblog stuff, but I think he wants open the chicken shed so they can wander off on certain times or something like that. I think he uses reed contacts to determine if the gate is open or shut. I myself use that technique for my Lego trains and also a model airballoon.

If you want to know how many turns a wormwheel goes I highly recommend disembowling an old printer. They contain what is known as a stepper motor. Stepper motors turn a certain number of degrees each step and hence you can calculate everything based on that. And they are REALLY powerful. I use one for my record cleaner. It can be any old printer.

1 Like

Try to keep the loop to just TWO lines.

Blynk.run();
SomeTimer.run();

Everything else should be run on timer loops unless you have a very good reason to include it in the loop.

Blynkers, I’m fascinated by you! This is the best community ever! :pray: :heart:

Was reading and the whole thread just made my day :slight_smile:

1 Like

Thanks for the advice that’s really helpful. I assume the SomeTimer in your example is an instance of SimpleTimer() ?
And can i then use several timers in the loop?
Blynk.run(); SomeTimer.run(); AnotherTimer.run(); MaybeEvenaThirdTimer.run();

or would you advise against that?

Yes SimpleTimer is generally the recommended one to use.

  timer.setInterval(60000L, CheckTimerThermo);  // check Timer and Thermo every 60 seconds(EVERY MINUTE)
  timer.setInterval(30000L, getTemperature);  // check temperature every 30 seconds
  timer.setInterval(7000L, refreshLCD); // refresh LCD if needed every 7 seconds
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds

My setup() is above and just one call to timer.run() in the loop.

You only need 1 timer, as @Costas says above. You can set different intervals, timings and routines with 1 timer object.

You can also individually enable and disable timers in the course of your program. For example, I have light detection with simple LDR’s and when the darkness sets in, I turn on some lights and disable a short timer which checks every 10s for the light levels. After that I turn a new 5-minute timer. So in effect the lights go on for at least 5 minutes, this is to prevent flickering of lights.

This is just one example of how extremely flexible the SimpleTimer library is. It saves a lot of headaches if you are using Blynk a lot :slight_smile:

I believe the limit is 10 occurrences with SimpleTimer but this should be more than enough for most projects. Try to keep the number of timers to a minimum so if you think you would like to do something every 5 seconds and something else every 7 seconds maybe combine the 2 and do them both every 6 seconds.

What I have found since finding Blynk is that my sketches are much more structured than in the pre Blynk days. I am not a coder so I just used to ramble on and on in the sketch without much thought to the structure.

The Blynk issue is that the server is checking for a hearbeat from your Arduino every 5 or 10 seconds depending which version of Blynk you use. If you miss a hearbeat you will be disconnected from the server. Try to avoid using delay() and if you must keep them as short as possible (sub 100 milliseconds).

5 to 10 seconds is quite a long time but it will soon disappear if your have delay(1000) scattered all over your sketch.

As well as your own functions called by the timer you will obviously have Blynk functions too. Such as:

BLYNK_WRITE(V0) { // plus time
  int PlusBtn = param.asInt();
  if (PlusBtn == 1) {
    dosomething();
  }
 else{
  dosomethingdifferent();
 }
}

With Blynk pin assignments you can decide how often the pin is checked between about 250 ms and 10 seconds plus PUSH frequency. Many of my buttons are momentary switches rather than on / off switches and I generally always select PUSH frequency. I have no idea how often PUSH is monitored but as it is determined by Blynk I work on the basis that they know the best frequency. The reason I mention this is that it isn’t just the heartbeat that you have to take care of. If you accidentally flood the server you will be disconnected.

Will you be using cloud or local server as I believe you can change the heartbeat timeouts and flood levels if you are running your own server? Personally I use the cloud.

Correction, buttons don’t have a frequency but other widgets do so take care when choosing the frequency in the app.

Hi,
For now i’ll be using the cloud server, one step at the time.
1 more question; the argument you give to the BLYNK_WRITE(V0) is that the pin number?
so if i want to listen to pin 7 would this be BLYNK_WRITE(V7)?

Yes the virtual pins start a zero so V0 and V7 for virtual pin 7. There are obviously analogue and digital pins too but most of my stuff is virtual (including the LCD).

There are a lot of advantages over using vPins rather then direct control of pins. You can use the vPin as wrapper and add more functionality with the push of a button.

Hi,
I read the docs but i’m not sure i understand the concept of virtual pins.
Why would i use a virtual pin? I only use Analogue and digital pins in my sketch.

tnx
RDG