First piston, variable problem


#1

1) Give a description of the problem
I am trying to make a piston to control some lamps with the push of a button. I was trying to change the temperature of the light with each push. I think the problem is the variable I have made, I’m not sure that I use the variable right.

2) What is the expected behaviour?
The lamps should change temperature with each push of the button. Until I push button 2, that turn off the light.

3) What is happening/not happening?
The lamps turns on when I press the button, but I cant control the temperature. They start at one temperature, but changes by them self.

**4) Post a Green Snapshot of the piston!

5) Attach logs after turning logging level to Full

### Logs

10/9/2019, 11:42:07 PM +90ms
+0ms ╔Received event [IKEA On/Off switch 1 top button].button = pushed with a delay of 33ms
+60ms ║RunTime Analysis CS > 13ms > PS > 31ms > PE > 16ms > CE
+62ms ║Runtime (41567 bytes) successfully initialized in 31ms (v0.3.10f.20190822) (61ms)
+63ms ║╔Execution stage started
+72ms ║║Comparison (enum) pushed gets (string) pushed = true (1ms)
+74ms ║║Cancelling condition #2's schedules...
+75ms ║║Condition #2 evaluated true (6ms)
+76ms ║║Cancelling condition #1's schedules...
+77ms ║║Condition group #1 evaluated true (state changed) (9ms)
+83ms ║║Comparison (integer) 0 is_equal_to (integer) 0 = true (2ms)
+85ms ║║Condition #15 evaluated true (6ms)
+86ms ║║Condition group #13 evaluated true (state did not change) (7ms)
+89ms ║║Cancelling statement #16's schedules...
+95ms ║║Calculating (integer) 0 + (integer) 1 >> (integer) 1
+101ms ║║Executed virtual command [Spisebord light 1, Spisebord light 2, Spisebord light 3].setVariable (2ms)
+116ms ║║Executed physical command [Spisebord light 1].setColorTemperature([2200]) (12ms)
+118ms ║║Executed [Spisebord light 1].setColorTemperature (13ms)
+132ms ║║Executed physical command [Spisebord light 2].setColorTemperature([2200]) (10ms)
+133ms ║║Executed [Spisebord light 2].setColorTemperature (13ms)
+146ms ║║Executed physical command [Spisebord light 3].setColorTemperature([2200]) (9ms)
+147ms ║║Executed [Spisebord light 3].setColorTemperature (12ms)
+154ms ║║Comparison (integer) 1 is_equal_to (integer) 1 = true (2ms)
+156ms ║║Condition #49 evaluated true (5ms)
+157ms ║║Condition group #48 evaluated true (state did not change) (7ms)
+160ms ║║Cancelling statement #50's schedules...
+166ms ║║Calculating (integer) 1 + (integer) 1 >> (integer) 2
+171ms ║║Executed virtual command [Spisebord light 1, Spisebord light 2, Spisebord light 3].setVariable (3ms)
+184ms ║║Executed physical command [Spisebord light 1].setColorTemperature([2700]) (9ms)
+185ms ║║Executed [Spisebord light 1].setColorTemperature (11ms)
+196ms ║║Executed physical command [Spisebord light 2].setColorTemperature([2700]) (8ms)
+197ms ║║Executed [Spisebord light 2].setColorTemperature (10ms)
+234ms ║║Executed physical command [Spisebord light 3].setColorTemperature([2700]) (36ms)
+235ms ║║Executed [Spisebord light 3].setColorTemperature (38ms)
+242ms ║║Comparison (integer) 2 is_equal_to (integer) 2 = true (2ms)
+244ms ║║Condition #58 evaluated true (5ms)
+245ms ║║Condition group #57 evaluated true (state did not change) (7ms)
+247ms ║║Cancelling statement #59's schedules...
+253ms ║║Executed virtual command [Spisebord light 1, Spisebord light 2, Spisebord light 3].setVariable (2ms)
+265ms ║║Executed physical command [Spisebord light 1].setColorTemperature([4000]) (9ms)
+266ms ║║Executed [Spisebord light 1].setColorTemperature (10ms)
+276ms ║║Executed physical command [Spisebord light 2].setColorTemperature([4000]) (9ms)
+277ms ║║Executed [Spisebord light 2].setColorTemperature (10ms)
+287ms ║║Executed physical command [Spisebord light 3].setColorTemperature([4000]) (8ms)
+288ms ║║Executed [Spisebord light 3].setColorTemperature (9ms)
+297ms ║║Cancelling condition #7's schedules...
+298ms ║║Condition #7 evaluated false (6ms)
+299ms ║║Cancelling condition #6's schedules...
+300ms ║║Condition group #6 evaluated false (state changed) (8ms)
+302ms ║╚Execution stage complete. (239ms)
+303ms ╚Event processed successfully (303ms)

If a solution is found for your question then please mark the post as the solution.


#2

Your ifs are sequential so by the time it gets to the next one you have already incremented your counter so they all end up executing. Move your increment to the end (after all the ifs) and it should work OK. Just put a check after incrementing so if >2, set to 0.


#3

Either that, or write the code backwards… Referencing 2 first, then 1, and finally 0.
(since the code executes from top to bottom at each execution)