Momentary backlight on toggle switch

What’s the easiest way to get an SvgSwitch that’s backlit (using createLightParamCentred etc etc) that displays the backlight with a momentary press (light turns on when depressed, turns off when released) but updates the parameter like a latching switch (toggles state when depressed only)?

I can’t seem to get it working particularly cleanly and can’t find any examples.

I was about to make something like this yesterday. Didn’t get around to it yet, though.

You can use setVoltage, instead of getVoltage, to set the state of both the button and the light for the button.

Think I am going to try using some kind of while loop to do this.

https://vcvrack.com/docs-v2/structrack_1_1engine_1_1Port#a2f0c9e3ac18bd538c1f99ea0c8f0de71

I got something working now :slight_smile:


Variables:
bool OnOff;
bool ParamSave;
int count;

Code in process block:


ParamSave = params[SAVE_PARAM].getValue();
if(ParamSave > 0){
OnOff = 1;
count = count+1;
if(count > (args.sampleRate*0.5)) OnOff = 0;
}
else{count = 0;}

params[SAVE_PARAM].setValue(OnOff);
lights[SAVE_LIGHT].setBrightness(OnOff);

This is the widget declaration for the button I use:

addParam(createLightParamCentered<VCVLightLatch<MediumSimpleLight<WhiteLight>>>(mm2px(Vec(10, 10)), module, TestOnly::SAVE_PARAM, TestOnly::SAVE_LIGHT));

For some reason the line formatting is messed up when I paste code in here.

Here is a screenshot of how it looks:

This will give you an on time for half a second. You can change the *0.5 to set another time. Times 1 is a second, times 0.25 is a quarter of a second, etc.

This site uses markdown, so wrap your code like this, and it will get formatted and colored correctly:

```cpp
<your code>
```

1 Like

Thanks, I appreciate it.

I tried using the blockquote thing, didn’t work great… :slight_smile:

Code blocks (triple back tick fences), not block quote (right angle bracket) which is for wrapping text quotes.

In other words: Code format

You can edit your post to fix it.

Ahh that helped :slight_smile:

Looks mucho better now :slight_smile:

1 Like

You’ve got the markdown right, but your code formatting style is awful ;-).

Haha, thanks :slight_smile:

I am not really a programmer by trade, so stil trying to find out what my “code formatting style” is :wink:

As long it works and I understand what’s going on, I am fine with it for now :slight_smile:

Then let your text editor re format it, please!

I use Atom. I find it pretty nice :slight_smile:

I do sometimes find the code I find around the web hard to read. I really hate when the editor start moving code without me doing it :sweat_smile:

Editors I’m familiar with have an option that you control that reformats a section or the whole file. But if you want to format your stuff so that it’s painful for others to look at you can certainly do that! I always say, the great thing about programming by yourself is that you can do whatever you want.

How should I format it then?

I certainly don’t want to inflict pain on anyone, using c++ :wink:

And also, if you have a suggestion to why this wont work as a function, I’d appreciate it. I got the samplerate correctly into my function.

I think the problem is in the counter. The counter doesn’t do anything or start counting, the same way it does when I do it straight in the process block.

There are a few conventions. I think ATM I just set my editor to use the “google c++ coding conventions”. Some ppl argue this a lot, but most everyone agrees that curly braces should indent a level, and that the code inside should indent, too.

Many ppl like the starting curly brace to live on the same line as the statement that caused it, like:

if (foo) {
    bar();
    if (foo2) {
        bar2();
    }
}

Other ppl put their open braces on the next line. Takes more space but the nesting can be easier to see. Perhaps less common these days(?)

if (foo)
{
    bar();
    if (foo2)
    {
        bar2();
    }
}

I would be very surprised if your editor didn’t have some normal default “code style”, and have some feature to apply it. I’m by nature sloppy, so I have my editor re-format my code pretty often.

Any way, google “formatting c++ braces” or “c++ coding conventions”, you will get a ton of hits.

Here is a small example. First thing I found in my git repro. It’s not a model of beauty, but it’s probably formatted “correctly” (that’s a joke, there is no “correct”). SqHarmony/util/ArpegPlayer.h at main · squinkylabs/SqHarmony · GitHub

I take your suggestion for code formatting to the heart and I appreciate it and I will think about it in the future, but atm, I am more focused on actually getting the function working.

No doubt I have a lot to learn about the syntax, but thats like learning a language and it take time. Right now I am just trying to spell a single word :wink:

And I have no clue why my counter doesn’t work when I make the code into a function.

1 Like

I tried to make a really simple example. If I do this is process block, it works. ParamSave is a bool button.

if(ParamSave){
Count = Count+1;
}
else{Count = 0;}

outputs[SINE_OUTPUT].setVoltage(Count);

That works perfectly. The ParamSave turn the counter on/off.

But if I make it into a function and have the function in a separate functions.cpp file, like this:

int Counter(bool On){
int count;

if(On > 0){
count = count+1;
}
else{count = 0;}

return count;
};

The functions declared like this in functions.hpp:

int Counter(bool);

And then call the function like this in the main:

Count = Counter(ParamSave);
outputs[SINE_OUTPUT].setVoltage(Count);

Nothing happens. Then counter doesn’t start. I know the reference to the function.cpp works, as I did a few other simpler math functions, like a+b, with no counter and they worked. But when I do this with a counter, it just doesn’t work.

There seems to be something with time, not being “active” in the functions.cpp. Like the time that’s in process block doesn’t exist in my functions.cpp file. Not really sure how to describe it beyond that, it’s like the functions.cpp is just a static file, that doesn’t do any timebased code, like a counter.

Is there anything I have to do to make time “exist” in the functions.cpp?

The count in your function is not initialized or saved anywhere between invocations. Why not debug it, even by putting in some kind of logging / printf / INFO? Just dump out count in the function.

This would be probably more like what you want if Counter were a member function of your module class/struct. Then count could be a member variable and would persist between calls. You almost certainly don’t want to make count be a global variable (the obvious “temporary” solution).

btw, us “experienced” programmers get stuff wrong all the time. You are going to need to learn how to roll up your sleeves and debug it just everyone else does. Very few programmers just write perfect code the first time…

I have been rolling up my sleeves. Just because I ask about a single issue here, which is related to functions and use in VCV, I am having trouble with, doesn’t mean I am having trouble with everything else.

During the last week, I build a phaser based “phaselocked” sequencer, with clock divider, length, offset, random step selection, freeze function, probability, retrigger function, with complete control over where the sequencer position at any given time. I did the user interface. I made an LFO that does the same. There is pretty much no sequencer who does anything close to this in VCV. Nysthi Programmer is the one that comes closest, but it also has its weak points, which I why I am writing my own personal sequencer.

I learned how to add presets and save them in json file. This momentary button, I posted code for above, which I did myself, is part of that presets system, for saving. I made it and shared it with the guy that made this thread, no questions asked, cause he needed it too and now I am just trying to simplifying my code in main process block by turning it into a function.

ALL of this on my own, with out asking a single question, just doing research on the web and looking in this forum. I have no sleeves left :wink:

BUT… I am new to using functions and there is clearly something that does not compute the same way in a function in VCV, as it does if the code is used directly in process block. And I am just trying to find out how it works.

No I am not a programmer, so there is absolutely stuff, I dont understand. But that’s how it is, we are on different levels and, I hope, that we all help as we have skills for. I helped this guy, in this thread. I also made a Launch controller LXL template and a patch for another guy in here a few days ago, cause I knew how to do that.

So you saying I have to learn to roll up my sleeves… I am gonna let it slide, cause you don’t know how much work I have been doing the last week or how much I’ve learned…

I see many people in these programming forums, who when someone asks about something, it ends up in long lectures about something that really had nothing to do with the issue the person asked about. I always wondered why that is… Why not spend the time that one would spend on the lecture, to actually help out? I just don’t understand that mentality.

About initialising the counter. I already tried that. Doesn’t do anything at all. If you look at the code says:

 if (input > 0){
count}
else{count = 0;}

So when the input 0, counter is reset to 0. The parameter send to input is initialised to zero, so the counter is also 0.

And also, once again, the code above works directly in process block, without the initialisation. Which ones again makes me think that there is a difference in using for example a counter in or outside process block.

The example I posted is as simple as it gets. if input on, then Count, if input off then set to 0. I use the output for debugging. I tried printf in the function, but it also only work sin process block, not in my function.cpp. I believe that if it was an initialisation issue, the counter would just start off where it left, so if it had reached 34567, it would just start of there next time input is 1. That’s what usually happens if a counter is not initialised. I did try to initialise it again and the output I get from the counter function is just 0 and 1 when I turn off/on the toggle button that’s fed to the function, the counter just doesn’t start at all. So I don’t think it’s an init issue.

Like Squinky, I suggest you use a debugger to step through your code a line at a time and watch the value of your variables change as each line is executed. This is more enlightening than dropping the occasional print statement. It helps develop your intuition of how code works.

From the discussion, I can see that you need to become familiar with the scope and lifetime of variables, the difference between local variables and member variables, and the difference between standalone functions (like your function in a separate file) and member functions like process().

1 Like