Yun strange Console debug values [closed][solved]

Hi there !

First of all i have to mention that english isn't my favorite language. :confused:
But i hope you can understand me or even help me a little with the problem i'm facing.

I got this code :

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

int statusled = 13;
int klingl = 2;
int s1on = 3;
int s1off = 4;
int s2on = 5;
int s2off = 6;
int s3on = 7;
int s3off = 8;
int s4on = 9;
int s4off = 10;
YunServer server;

void setup() {
  pinMode(statusled, OUTPUT);
  pinMode(klingl, OUTPUT);
  pinMode(s1on, OUTPUT);
  pinMode(s1off, OUTPUT);
  pinMode(s2on, OUTPUT);
  pinMode(s2off, OUTPUT);
  pinMode(s3on, OUTPUT);
  pinMode(s3off, OUTPUT);
  pinMode(s4on, OUTPUT);
  pinMode(s4off, OUTPUT);
  digitalWrite(statusled,HIGH);
  Bridge.begin();
  Console.begin();
  server.begin();
  digitalWrite(statusled,LOW);
}

void loop() {
   YunClient client = server.accept();
  if (client) {
    String command = client.readStringUntil('/');
    command.trim();
    Console.println("Command : " + command);
    if (command == "klingl") {
       Console.println("Schalte klingl");
       digitalWrite(statusled,HIGH);
       digitalWrite(klingl,HIGH);
       delay(3000);
       digitalWrite(klingl,LOW);      
     }
     if (command == "hzgan") {
       schalte(s2on);
     }
     if (command == "hzgaus") {
       schalte(s2off);
     }
     if (command == "stereoa") {
       schalte(s1on);
     }
     if (command == "stereoo") {
       schalte(s1off);
     }
     if (command == "ban") {
       schalte(s3on);
     }
     if (command = "baus") {
       schalte(s3off);
     }
    
   }
}

void schalte(int whatto) {
       Console.println("Schalte " + whatto);
       digitalWrite(statusled,HIGH);
       digitalWrite(s3off,HIGH);
       delay(300);
       digitalWrite(s3off,LOW);
       digitalWrite(statusled,LOW);
}

It triggers a Button on a remote control for sockets (jacks - the thing the current (230~) comes out).
If i try the programm with just one or two commands all work fine but if i use all commands
in the void loop (inside the if (client)) the programm stops working and i get some strange debug values.

Command : klingl
Schalte klingl

Command : klingl
Schalte klingl

Command : hzgan
te

Command : hzgaus
e

Command : hzgaus
e

Command : hzgan
te

Command : stereoa
alte

Command : hzgaus
e

As you can see the function schalte isnt working correctly and aborting while running the.
The digitalWrite command isn't triggert and the switches doesn't work at all.
(As you can see in the first part of the debugging all is running as usual.
This was the test with only 2 commands)

I have no idea what im doing wrong and im in programming since 2009.
Is there a known bug ?
Is the microcontroller to slow ?
I have no idea... i hope i'm right here and someone knows the answer...

Thank you for reading (and maybe even understanding) !

Nachid

The key is here:

void schalte(int whatto) {
       Console.println("Schalte " + whatto);

whatto is an integer.

"Schalte " is a constant character array, which can also be considered a pointer to a constant character, pointing to the first character of the string.

Console.println expects a parameter that is a pointer to the first character to print.

"Schalte " + whatto is NOT converting whatto to a string and then appending it to "Schalte " but instead is doing pointer arithmetic. It is taking the pointer to the beginning of the string, and the ADDING whatto to it, and then the resulting pointer gets passed to println.

So, if whatto is zero, it will print "Schalte "
If whatto is three, it will print "alte " (skipping the first three characters)

Replace the single println call with:

       Console.print("Schalte ");
       Console.println(whatto);

The first line will call the version of print that takes a character string and prints it. The second line will call the version of println that takes an int, converts it to a string, then prints it.

Yeah sure i got it... its only i've been working with none objectiv languages all the time where this command would not be an arithmetic operation.

Thank you for your patience.

I found another bug here if some1 want to use this :

void schalte(int whatto) {
       Console.println("Schalte " + whatto);
       digitalWrite(statusled,HIGH);
       digitalWrite(whatto,HIGH);
       delay(300);
       digitalWrite(whatto,LOW);
       digitalWrite(statusled,LOW);
}

s3... have to be whatto to make this thing work correctly.

and in the code above this function

if (command = "baus") {

have to be

if (command == "baus") {

Nachid:
Yeah sure i got it... its only i've been working with none objectiv languages all the time where this command would not be an arithmetic operation.

This would've actually worked if "Schalte " had been a String object, instead of a C++ string (note the difference in capitalization.) The String class does have an addition operator that allows adding an integer to a string (and converting the integer into a string at the same time. This would've worked, at the expense of bringing in the String class and adding in more overhead: (and there are those who avoid the String class at all costs)

Console.println(String("Schalte ") + whatto);

s3... have to be whatto to make this thing work correctly.

I saw that, I figured you had changed it because you didn't trust that whatto had the right data and you wanted to be able to see something happening on a known pin. :wink:

and in the code above this function

if (command = "baus") {

have to be

if (command == "baus") {

Good catch! I wasn't looking at that closely (and I stopped looking once I saw the pointer arithmetic.)

As i said in batch / php (i think it works there too) / autoit and so on the + or & works between Strings and Ints or longs and u dont have to define them. They are just defined by the type the get. Ty very much for your help :slight_smile: