MATLAB: Use a button to increase a value in simulink each time it is pressed

simulink

I am trying to simulate shifting gears in simulink. I want to increase a constant each time it is pressed. I have 6 dashboard lamps that I am going to use as gear indicators.
Thank you.

Best Answer

Alright most people probably already know how to do this but I finally figured it out. It is just done through a triggered subsystem that uses a matlab function. In my case, a button was pressed that made a constant block go from low to high (trigger). Then another constant block is fed into the matlab function that uses get_param(constant2) and set_param(constant2+1) to shift the constant block up one value.
Then you just connect your lamp to the constant block that is changing and you have your solution.
Biggest things learned: constant blocks need to be stored as a string so str2num and num2str must be used in parameter setting; trigger subsystem just acts once per activation! Also you can ask and answer your own questions in MATLAB forums!
function Gears
coder.extrinsic('get_param');
coder.extrinsic('set_param');
coder.extrinsic('str2num');
coder.extrinsic('num2str');
current=0; new =0;
current=str2num(get_param('TachStart/Gearcurrent','Value'));
if (current==1)
new = current+2;
set_param('TachStart/Gearcurrent','Value',num2str(new));
elseif (current==2)
new = current+1;
set_param('TachStart/Gearcurrent','Value',num2str(new));
elseif (current==3)
new = current+1;
set_param('TachStart/Gearcurrent','Value',num2str(new));
elseif (current==4)
new = current+1;
set_param('TachStart/Gearcurrent','Value',num2str(new));
elseif (current==5)
new = current+1;
set_param('TachStart/Gearcurrent','Value',num2str(new));
else
return
end