Arduino as a logic switching device

Hi.

I would like to build a device that would have 5 switches atteched to digital inputs or analog inputs.
And it would get an external Square wave signal.
It would have one analog output with x voltage

If I press the first switch, I get x+1 voltage on the analog output and if I let go of the siwtch it would turn the analog output back to x but only when the external signal pulses. So it basically waits when I disengage the switch and the signal pulse and returns to the specified value.
And so on with all the other switches.

Could some one help me with the code?

Thank you!

How fast is the external square wave?

You realize analogWrite (pinX, Z); where Z is a number from 0 to 255,
outputs a PWM signal switched at 490 Hz with the width from full off, to 1/255 increments of the 490 Hz period, to full on? It’s not an analog level out.

So you could write a pretty simple while loop:

squareWave = digitalRead(squareWavePin);
while (squareWaveP == HIGH){
squareWave = 2;
if ( digitalRead (pin1) == HIGH){switch1 = 4;}
else {switch1 = 0;}
if ( digitalRead (pin2) == HIGH){switch2 = 8;}
else {switch2 = 0;}
if ( digitalRead (pin3) == HIGH){switch3 = 16;}
else {switch3 = 0;}
if ( digitalRead (pin4) == HIGH){switch4 = 32;}
else {switch4 = 0;}
if ( digitalRead (pin5) == HIGH){switch5 = 64;}
else {switch5 = 0;}

analogWrite(PWMpin, ( (switch5 | switch4 | switch3 | switch2 | switch1 | squareWave );
squareWave = digitalRead(squareWavePin);
}

There are faster ways to do this, using direct port manipulation would be one way, but this should get you on a path that works.
The analog output is a PWM signal that you would need to put thru a low pass filter to turn into a DC level kind of signal.

Hi.

The Square wave signal is very low frequenzy from 30bpm to 250bpm. (0,5Hz to 4,17Hz)
This frequenzy is pre set by an external device and will not vary during the operation.

I will build the LPF. Thank you!
Now I will go and read some coding to figure out what you did there.
Thanks for help!