BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Problem adding switch case to terminal for max7219

hi im having troubles making the widget switch case (v5) work to chose between two options, ex: case1 for printing whatever its written on the widget “terminal” (v6) and send it to the display max7219 and in case 2, send just the word “message” i did receive the message on the serial monitor but not the max7219.and the max7219 its working fineive allready print a message before i decide to add the widget switch button. what am i doing worng on the code? thank you very much


#define BLYNK_PRINT Serial
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define CLK_PIN   D5
#define DATA_PIN  D7
#define CS_PIN    D8
char auth[] = "...";

char ssid[] = " ";
char pass[] = " ";
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
 P.begin();
  Blynk.begin(auth, ssid, pass);
  }
 

}
 BLYNK_WRITE(V5){//switch button
switch (param.asInt())
{
  case 1: 
{  BLYNK_WRITE (V6); //terminal
P.print (param.asStrg());
Serial.println (param.asStrg());
  break;
}
case 2: {

  P.print ("message");
  Serial.println ("message");
  break;
}}}


void loop() {
  // put your main code here, to run repeatedly:
 Blynk.run();
 




}

@juanmanuel85 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly correctly.
Triple backticks look like this:
```

Pete.

1 Like

Exactly what type of widget is attached to pin V5, and how is it configured in the app?

Pete.

1 Like

First - Read how switch must work.
Second - try to write data from server to variable and print it. E.g.

BLYNK_WRITE(V5){//switch button
int var = param.asInt();
Serial.print(var);
switch (var);
....
}

Most likely you will find, that you can get only values 0 and 1 when you will press your switch button, and never value 2. So, your case 2 will never work. Etc.

1 Like

I guess he’s using the segmented switch here.

1 Like

hi yes its the segmented switch in v5
and the terminal in v6 to wite and send data. ive allready printed in the max7219 by writing just “blynk_write (v6), p.print (param.asStg()” so ive tried to add that to the swoitch but didnt work

segmented switch in v5
and terminal v6

Your code is a mess.
You have a closing curly bracket before your BLYNK_WRITE(V5), BLYNK_WRITE(V6) embedded within BLYNK_WRITE(V5) and curly brackets within your switch…case statement that don’t need to be there.

Pete.

I guess, you should separate routines BLYNK_WRITE(V5) and BLYNK_WRITE(V6) and relay data from one to another. I.e.

define string variable
 
BLYNK_WRITE(V5) 
{
print string variable  here;
}

BLYNK_WRITE(V6)
{
get string variable here;
}

I’m afraid, it’s not a good idea to use expresion “param.asStrg()” several times in the same routine instead of corresponding to it variable.

ie allready did that in other code and worked fine with max7219 problem was adding the switchcase to make it work with the segmented switch i dont know how to include well those routines with the switch case part

Do not hesitate to use Serial.println() expressions in point where you think you can have some problem. At any moment you can comment them out. But they usually can be helpful to figure out what your code really do.

P.S. I mean not empty Serial prints, of course :slight_smile: , but something like Serial.println(“Point 3”) or Serial.println("variable x = " … )

You cant put one BLYNK_WRITE(vPin) inside another. These functions are special Blynk callbacks that only trigger when the widget value changes.

If you want to capture the value from one widget (say the terminal) then you need to make the BLYNK_WRITE(vPin) for that widget save the value to a global variable. In the case of the terminal this will be a string, and this is not the correct syntax…

It should be param.asString()

Pete.

Hey there, try this :

BLYNK_WRITE(V6) {

  String value = param.asStr();
}

BLYNK_WRITE(V5) {
  switch (param.asInt())
  {
    case 1: // Item 1
      P.print (param.asStr());
      Serial.println(param.asStr());
      break;
    case 2: // Item 2
      P.print ("message");
      Serial.println("message");
      break;
  }
}

Don’t forget to define Value as a global variable if you want to use it outside the BLYNK_WRITE(V6) function.

No. In this piece of code

BLYNK_WRITE(V5) {
switch (param.asInt())
  {
    case 1: // Item 1
      P.print (param.asStr());
      Serial.println(param.asStr());

in case 1 will be printed value of switch. i.e. “1”. juanmanuel85 want to print data received from terminal. So,

String terminal_value = "terminal is empty now";

BLYNK_WRITE(V6)  //get data from terminal
{
  terminal_value = param.asStr();
}

BLYNK_WRITE(V5) //print message depends on switch
 {
  switch (param.asInt())
    {
    case 1: // Item 1
      P.print (terminal_value);
      Serial.println(terminal_value);
      break;
    case 2: // Item 2
      P.print ("message");
      Serial.println("message");
      break;
    }
}

P.S. BTW, this code seems to be not quite functional when switch is in idle state. I.e. when switch is moved, something will be printed depends on switch. To print terminal data in real time it is worth to add Blynk.syncVirtual(V5) in code i.e.

BLYNK_WRITE(V6)  //get data from terminal
{
terminal_value = param.asStr();
Blynk.syncVirtual(V5) ;
}
2 Likes