Output problem with while - C++

I got it to work well, but when doing some tests I discovered that if I take the T value of a port A it works, but if that value left it fixed, just one cycle of while

#define unsigned long T;
void loop(){

T= analogRead(A0);  //map of 1 a 100
................................
while(){
................................
}
}
{{ GENERATED_CODE }}

void evaluate(Context ctx) {

     unsigned long T = getEvaluate<input_SIG>(ctx); // mapped signal 1 -100 **works**
     unsigned long T = 50;  // **it does not work**
     while() {
......................................
    emitValue<output_OUT>(ctx, .....
    }
}

If I take the signal, the mapping from 1 to 100 works.
If I assign T a value of 50 only a while ago, surely when changing the port the value oscillates and works for those things that happen. But I’m missing the loop, is it possible in XOD?
I already put it like that but nothing worked.

void loop() {
.........................
while(){
.....................................
}
}
{{ GENERATED_CODE }}
void evaluate(Context ctx) {

Solved:

    if (!getValue<input_UPD>(ctx)){
    T = T + 1;
    return;
    }
    T = T - 1;

I can not get the signal out, I tried like this …with pwmRange

    while(angulo < 2 * PI) {
    angulo += deltaAngulo;
    delay(_delay);
    duty = int(sin(angulo)*128+127);
    duty = duty > 255 ? 1 : (duty < 0 ? 0 : duty);
    int val = (int)(duty * pwmRange);
    ::pinMode(port, OUTPUT);
    ::analogWrite(port, val);
    }

and I tried like this …

    while(angulo < 2 * PI) {
    angulo += deltaAngulo;
    delay(_delay);
    emitvalue<output_OUT>(ctx, int(sin(angulo)*128+127));
    }

Hello!

To be honest, I don’t quite understand what are you trying to achieve. A sine wave generator? If so, have you seen xod/core/sine-wave? Regardless of anything, you are using delay in the while loop which will lead to unwanted consequences.

The delay function wastes CPU cycles and blocks everything (including evaluation of other parts of the program) until the time is elapsed. You have to refactor the evaluate implementation to be called more often (see setTimeout) and without any delay calls.

Hi, sine-wave node works, but after a day the behavior is erratic. At least in my tests.

This is a program that I found and is already working correctly,
one problem was the data type of the variable and another the map value at 255 should be 254000.

That’s right, the cycles are from 0.5 sec to 10 sec, not very useful

:open_mouth: sounds like a bug related to an overflow or float precision. Would you elaborate? Does it go crazy in a moment, right after 49 hours (2³² ms overflow) or degrade slowly after a few hours?

I thought so, I programmed it and I left it with an LED, the next day the cycle was not the same.

I will repeat it to be sure

@nkrkv , I went back to try sine-wave.

  • Patch with a sine wave node and a led node
  • Frequency 1hz

Seven hours later, the frequency is about 0.25hz

Thanks for the report. I can confirm the bug and created an issue on GitHub.

1 Like