Parameter passing

Hi all! I want to make a pulse width modulator where ‘0’ is 100msec long low level and ‘1’ is 200msec long low level. To do it the ‘slice’ function is attached to timer interrupt to book time slices in ‘segment’ variable. The pulse width is contolled in case 1 depending on ‘pulse’ variable.
In the loop ‘pulse’ variable is toggleg but interrupt does not see it. However it is decrared volatile. What is wrong with this code?

#include <Ticker.h>
Ticker timerIT;

#define dpin 2

volatile uint8_t segment = 0 ; // n*100 msec
volatile uint8_t pulse = 0 ; // pulse width
volatile uint8_t level = 0 ; // level to be outplaced

void setup() {
pinMode(dpin, OUTPUT);
digitalWrite(dpin, HIGH);
timerIT.attach(0.1, slice);

void slice()
digitalWrite(dpin, level);
switch (segment)
case 0:
level = 0;
case 1:
if (pulse == 0) level = 1;
case 10:
segment = 0;
level = 1;
level = 1;

void loop ()
pulse ^= 1;

What do you mean “doesn’t see it”?
You’ve got a “timer” going off at 100ms intervals and a loop that’s running hundreds of thousands of times a second.

Please remember to use code tags when posting code.

How is segment ever 0 when the switch executes?


I conclude that after the first iteration (when level starts out initialised as 0) level can never be 0 again after it is set to 1.

#1 What do you mean "doesn't see it"?

Please refer to subject. But in details:
in the loop "pulse" is toggled continously. In the interrupt routine case 0 sets low level and case 1 set high level - or leave it for next case, which is the default branch. It will set high level.
However the changing 'pulse value' does no result pin change.

The segment is incremented BEFORE the switch. When it reaches 10, then will be set back to zero, etc.

#1 What do you mean “doesn’t see it”?

Please refer to subject.

Which parameters?
Passing to where?

case 0 sets low level

And as I pointed out can never execute because the minimum value segment is ever set to is zero, but there’s a “increment by one” right before the switch.

level can never be 0 again after it is set to 1.
You are right, thank you. When segment reaches ten, then I reset it to zero, but simultaneously set level is correctly 0. Thanks for you pointing out!