Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Keeping the lights on/off
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RahulPatel23




PostPosted: Fri May 17, 2013 9:39 am   Post subject: Keeping the lights on/off

What is it you are trying to achieve?
I have to simulate a program that would allow you to remotely turn on/off lights for my Computer Engineering culminating assignment.

This is my first semester coding so I apologize in advance for any stupid mistakes i make Embarassed

Thanks in advance Very Happy
What is the problem you are having?
Whenever I click to turn the lights on they immediately reset to "off"

Describe what you have tried to solve this problem
Tried removing various loops and cls's but thats about it.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


for decreasing x : 5 .. 1 %puts in intro text
    locate (12, 25)
    put "Welcome to the Light Switcher."
    locate (13, 10)
    put "Click on the green or red areas to turn the lights on or off."
    locate (14,15)
    put "Click the yellow box to toggle the master power."
    locate (15, 17)
    put "Click on the red 'X' to quit the program."
    locate (16, 30)
    put "Please wait... ", x
    delay (1000)
    cls
end for

View.Set ("offscreenonly")
loop
    var x, y, button : int
    x := 0
    y := 0
    var livingcolour, bedcolour : int
    livingcolour := 118
    bedcolour := 113
    Mouse.Where (x, y, button)
    if x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 then % if the mouse is clicked within the green box then the green light will switch on
        livingcolour := 47
       
    elsif x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 then % if the mouse is clicked within the red box then the red light will switch on
        bedcolour := 40

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 then % if the mouse is clicked within the yellow box then both lights will switch on
        bedcolour := 40
        livingcolour := 47
    end if

    %Colouring in the rooms
    Draw.FillBox (50, 340, 299, 30, livingcolour)
    Draw.FillBox (550, 340, 300, 30, bedcolour)

    %Draws outermost walls
    Draw.ThickLine (550, 340, 50, 340, 4, black)
    Draw.ThickLine (50, 340, 50, 30, 4, black)
    Draw.ThickLine (50, 30, 550, 30, 4, black)
    Draw.ThickLine (550, 30, 550, 340, 4, black)

    %Draws dividing wall
    Draw.ThickLine (300, 340, 300, 220, 2, black)
    Draw.ThickLine (300, 30, 300, 180, 2, black)

    %Draws exit sign
    Draw.Box (560, 30, 610, 80, black)
    Draw.Line (610, 80, 560, 30, black)
    Draw.Line (560, 80, 610, 30, black)
    Draw.Fill (561, 32, 40, black)
    Draw.Fill (565, 32, 40, black)
    Draw.Fill (591, 71, 40, black)
    Draw.Fill (601, 61, 40, black)

    %Draws Master Power Switch
    Draw.ThickLine (610, 130, 560, 130, 3, black)
    Draw.ThickLine (610, 130, 610, 230, 3, black)
    Draw.ThickLine (610, 230, 560, 230, 3, black)
    Draw.ThickLine (560, 130, 560, 230, 3, black)
    Draw.Fill (571, 201, yellow, black)

    %Clears and resets the screen every time it loops
    View.Update
    cls


    if x >= 560 and x <= 610 and y >= 30 and y <= 80 and button = 1 then %if the mouse is clicked within the red box w/ an "x" then the program will quit
        cls
        locate (12, 25)
        put "Thanks for using Light Switcher!"
        exit
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri May 17, 2013 9:53 am   Post subject: RE:Keeping the lights on/off

Throw in a boolean statement that keeps track of whether the lights have been turned on or off.

That way you can change how the drawing works too. You draw according to what your booleans are. For instance, for your green bed could be something like this:

Turing:

var greenlight : boolean := false

loop
     if green_is_clicked () then
          %change from true to false, or false to true
     end if

     if greenlight = true then
          Draw.Box (x, y, x2, y2, brightgreen)
     else
          Draw.Box (x, y, x2, y2, green)
     end if
end loop


I made a functional program for you, but I don't quite want to give it to you yet, I'd rather you try to figure it out first.

Feel free to ask any other questions
TW iz Rippin




PostPosted: Fri May 17, 2013 10:50 am   Post subject: RE:Keeping the lights on/off

No! Don't do a boolean! The problem is that every time the loop runs through, it resets the colours to defuallt:

var livingcolour, bedcolour : int
livingcolour := 118
bedcolour := 113

those lines should be at the start of your program, rather than inside the main loop. Then when you click a different box, other than the one that is lit, recolour the box that was already lit.
Raknarg




PostPosted: Fri May 17, 2013 11:45 am   Post subject: RE:Keeping the lights on/off

Yes, but it makes more sense to use a boolean to keep track of your lights.

Anyways, either way it'll be the same thing... basically make sure you state your variables outside the loop unless you have a good reason for doing so.
RahulPatel23




PostPosted: Fri May 17, 2013 2:09 pm   Post subject: RE:Keeping the lights on/off

Alright thanks guys I will be sure to try your fixes when i get to class

Rahul
RahulPatel23




PostPosted: Sat May 18, 2013 4:25 pm   Post subject: RE:Keeping the lights on/off

@Tw iz Rippin
I tried putting the variables out side the loop and they can stay on, but if i want turn them back off, i can't.
RahulPatel23




PostPosted: Sat May 18, 2013 4:26 pm   Post subject: Re: RE:Keeping the lights on/off

Raknarg @ Fri May 17, 2013 9:53 am wrote:
Throw in a boolean statement that keeps track of whether the lights have been turned on or off.

That way you can change how the drawing works too. You draw according to what your booleans are.


i tried using booleans but im slightly confused Question, how exactly do they work?

Rahul
Nathan4102




PostPosted: Sat May 18, 2013 5:01 pm   Post subject: RE:Keeping the lights on/off

A boolean is a variable type, much like a string or an integer. You create a boolean variable the same way you create other variables:

Turing:
var OnOrOff : boolean


Booleans have two states: true, or false(No caps). They can be assigned states just like other variables:

Turing:
OnOrOff := true


You can use this to control weather your lights are on or off. Make sense now?
Sponsor
Sponsor
Sponsor
sponsor
RahulPatel23




PostPosted: Tue May 21, 2013 1:07 pm   Post subject: RE:Keeping the lights on/off

alright so i tried using a boolean for my livingroom lights but turing keeps saying "syntax error at ':=' expected 'then'"

setscreen ("graphics")
var font : int
font := Font.New ("serif:6")

Turing:


procedure grid
    for x : 10 .. maxx by 10
        for y : 10 .. maxy by 10
            drawdot (x, y, black)
        end for
    end for
    for x : 10 .. maxx by 20
        Draw.Text (intstr (x), x - 3, maxy - 6, font, black)
    end for
    for y : 20 .. maxy by 20
        Draw.Text (intstr (y), maxx - 12, y - 3, font, black)
    end for
end grid
for decreasing x : 5 .. 1 %puts in intro text
    locate (12, 25)
    put "Welcome to the Light Switcher."
    locate (13, 10)
    put "Click on the green or red areas to turn the lights on or off."
    locate (14, 15)
    put "Click the yellow box to toggle the master power."
    locate (15, 17)
    put "Click on the red 'X' to quit the program."
    locate (16, 30)
    put "Please wait... ", x
    delay (1000)
    cls
end for

var x, y, button : int
x := 0
y := 0

var livingcolour, bedcolour : int
livingcolour := 118
bedcolour := 113

var onoroff : boolean := false
View.Set ("offscreenonly")
loop

    Mouse.Where (x, y, button)
    if x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 and onoroff=false then % if the mouse is clicked within the green box then the green light will switch on
       onoroff:=true

    elsif x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 then % if the mouse is clicked within the red box then the red light will switch on
        bedcolour := 40

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 then % if the mouse is clicked within the yellow box then both lights will switch on
        bedcolour := 40
        livingcolour := 47
    end if
if onoroff := false then
livingcolour:=118 %this is where the error comes
elsif onoroff := true then
livingcolour:=47
end if
   
    %Colouring in the rooms
    Draw.FillBox (50, 340, 299, 30, livingcolour)
    Draw.FillBox (550, 340, 300, 30, bedcolour)

    %Draws outermost walls
    Draw.ThickLine (550, 340, 50, 340, 4, black)
    Draw.ThickLine (50, 340, 50, 30, 4, black)
    Draw.ThickLine (50, 30, 550, 30, 4, black)
    Draw.ThickLine (550, 30, 550, 340, 4, black)

    %Draws dividing wall
    Draw.ThickLine (300, 340, 300, 220, 2, black)
    Draw.ThickLine (300, 30, 300, 180, 2, black)

    %Draws exit sign
    Draw.Box (560, 30, 610, 80, black)
    Draw.Line (610, 80, 560, 30, black)
    Draw.Line (560, 80, 610, 30, black)
    Draw.Fill (561, 32, 40, black)
    Draw.Fill (565, 32, 40, black)
    Draw.Fill (591, 71, 40, black)
    Draw.Fill (601, 61, 40, black)

    %Draws Master Power Switch
    Draw.ThickLine (610, 130, 560, 130, 3, black)
    Draw.ThickLine (610, 130, 610, 230, 3, black)
    Draw.ThickLine (610, 230, 560, 230, 3, black)
    Draw.ThickLine (560, 130, 560, 230, 3, black)
    Draw.Fill (571, 201, yellow, black)

    %Clears and resets the screen every time it loops
    View.Update
    cls


    if x >= 560 and x <= 610 and y >= 30 and y <= 80 and button = 1 then %if the mouse is clicked within the red box w/ an "x" then the program will quit
        quit
    end if


    %grid
end loop



any help??
Nathan4102




PostPosted: Tue May 21, 2013 1:22 pm   Post subject: RE:Keeping the lights on/off

When comparing two things (In this case, "onoroff" and "false"), you need to use =. := is for assignment, when you want to assign a variable a value.
RahulPatel23




PostPosted: Tue May 21, 2013 1:41 pm   Post subject: Re: Keeping the lights on/off

Turing:

for decreasing x : 5 .. 1 %puts in intro text
    locate (12, 25)
    put "Welcome to the Light Switcher."
    locate (13, 10)
    put "Click on the green or red areas to turn the lights on or off."
    locate (14, 15)
    put "Click the yellow box to toggle the master power."
    locate (15, 17)
    put "Click on the red 'X' to quit the program."
    locate (16, 30)
    put "Please wait... ", x
    delay (1000)
    cls
end for

var x, y, button : int
x := 0
y := 0

var livingcolour, bedcolour : int
livingcolour := 118
bedcolour := 113

var gonoroff : boolean := false %the variable keeps track of whether the green light is on or off
var ronoroff : boolean := false %the variable keeps track of whether the red light is on or off


View.Set ("offscreenonly")
loop

    Mouse.Where (x, y, button)
    if x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 and gonoroff = false then % if the mouse is clicked within the green box then the green light will switch on
        gonoroff := true

    elsif x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 and gonoroff = true then     % if the mouse is clicked within the green box then the green light will switch on
        gonoroff := false
    end if
    if x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 and ronoroff = true then     % if the mouse is clicked within the red box then the red light will switch on
        ronoroff := false
    elsif x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 and ronoroff = false then     % if the mouse is clicked within the red box then the red light will switch on
        ronoroff := true
    end if

    if x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = false and ronoroff = false then
        % if the mouse is clicked within the yellow box then both lights will switch on or off
        gonoroff := true
        ronoroff := true

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = true and ronoroff = true then

        gonoroff := false
        ronoroff := false

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = false and ronoroff = true then

        gonoroff := true
        ronoroff := true
    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = true and ronoroff = false then

        gonoroff := true
        ronoroff := true
    end if


    if gonoroff = false then     %toggles the living room lights
        livingcolour := 118
    elsif gonoroff = true then
        livingcolour := 47
    end if

    if ronoroff = false then     %toggles the bedroom lights
        bedcolour := 113
    elsif ronoroff = true then
        bedcolour := 40
    end if

    %Colouring in the rooms
    Draw.FillBox (50, 340, 299, 30, livingcolour)
    Draw.FillBox (550, 340, 300, 30, bedcolour)

    %Draws outermost walls
    Draw.ThickLine (550, 340, 50, 340, 4, black)
    Draw.ThickLine (50, 340, 50, 30, 4, black)
    Draw.ThickLine (50, 30, 550, 30, 4, black)
    Draw.ThickLine (550, 30, 550, 340, 4, black)

    %Draws dividing wall
    Draw.ThickLine (300, 340, 300, 220, 2, black)
    Draw.ThickLine (300, 30, 300, 180, 2, black)

    %Draws exit sign
    Draw.Box (560, 30, 610, 80, black)
    Draw.Line (610, 80, 560, 30, black)
    Draw.Line (560, 80, 610, 30, black)
    Draw.Fill (561, 32, 40, black)
    Draw.Fill (565, 32, 40, black)
    Draw.Fill (591, 71, 40, black)
    Draw.Fill (601, 61, 40, black)

    %Draws Master Power Switch
    Draw.ThickLine (610, 130, 560, 130, 3, black)
    Draw.ThickLine (610, 130, 610, 230, 3, black)
    Draw.ThickLine (610, 230, 560, 230, 3, black)
    Draw.ThickLine (560, 130, 560, 230, 3, black)
    Draw.Fill (571, 201, yellow, black)

    %Clears and resets the screen every time it loops
    View.Update
    cls


    if x >= 560 and x <= 610 and y >= 30 and y <= 80 and button = 1 then     %if the mouse is clicked within the red box w/ an "x" then the program will quit
        quit
    end if


    %grid
end loop


so i figured out how to do the booleans but when i click the respective areas sometimes the lights wont toggle and will just flicker.
Also when it is successful when the toggle is happening there is still some flicker.
Are there any fixes for it?
Raknarg




PostPosted: Wed May 22, 2013 10:04 am   Post subject: RE:Keeping the lights on/off

Yes.

Let's say you press the button, and then the loop loops twice before you let go. The first time it goes through, it'll turn a light on. However, it will go through it again and then turn it off. So the problem is that you have to find a way to make it so that the program only turns the button on or off once while you're clicking, or when you let go of the button.
RahulPatel23




PostPosted: Wed May 22, 2013 10:25 am   Post subject: RE:Keeping the lights on/off

Turing:

for decreasing x : 5 .. 1 %puts in intro text
    locate (12, 25)
    put "Welcome to the Light Switcher."
    locate (13, 10)
    put "Click on the green or red areas to turn the lights on or off."
    locate (14, 15)
    put "Click the yellow box to toggle the master power."
    locate (15, 17)
    put "Click on the red 'X' to quit the program."
    locate (16, 30)
    put "Please wait... ", x
    delay (1000)
    cls
end for

var x, y, button : int
x := 0
y := 0

var livingcolour, bedcolour : int
livingcolour := 118
bedcolour := 113

var gonoroff : boolean := false %the variable keeps track of whether the green light is on or off
var ronoroff : boolean := false %the variable keeps track of whether the red light is on or off


View.Set ("offscreenonly")
loop

    Mouse.Where (x, y, button)
    if x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 and gonoroff = false then % if the mouse is clicked within the green box then the green light will switch on

        gonoroff := true

    elsif x >= 50 and x <= 300 and y >= 30 and y <= 340 and button = 1 and gonoroff = true then     % if the mouse is clicked within the green box then the green light will switch on

        gonoroff := false
    end if
    if x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 and ronoroff = true then     % if the mouse is clicked within the red box then the red light will switch on

        ronoroff := false
    elsif x >= 300 and x <= 550 and y >= 30 and y <= 340 and button = 1 and ronoroff = false then     % if the mouse is clicked within the red box then the red light will switch on

        ronoroff := true
    end if

    if x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = false and ronoroff = false then
        % if the mouse is clicked within the yellow box then both lights will switch on or off

        gonoroff := true
        ronoroff := true

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = true and ronoroff = true then

        gonoroff := false
        ronoroff := false

    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = false and ronoroff = true then

        gonoroff := true
        ronoroff := true
    elsif x >= 560 and x <= 610 and y >= 130 and y <= 230 and button = 1 and gonoroff = true and ronoroff = false then

        gonoroff := true
        ronoroff := true
    end if


    if gonoroff = false then     %toggles the living room lights

        livingcolour := 118
    elsif gonoroff = true then

        livingcolour := 47
    end if

    if ronoroff = false then     %toggles the bedroom lights

        bedcolour := 113
    elsif ronoroff = true then

        bedcolour := 40
    end if

    %Colouring in the rooms
    Draw.FillBox (50, 340, 299, 30, livingcolour)
    Draw.FillBox (550, 340, 300, 30, bedcolour)

    %Draws outermost walls
    Draw.ThickLine (550, 340, 50, 340, 4, black)
    Draw.ThickLine (50, 340, 50, 30, 4, black)
    Draw.ThickLine (50, 30, 550, 30, 4, black)
    Draw.ThickLine (550, 30, 550, 340, 4, black)

    %Draws dividing wall
    Draw.ThickLine (300, 340, 300, 220, 2, black)
    Draw.ThickLine (300, 30, 300, 180, 2, black)

    %Draws exit sign
    Draw.Box (560, 30, 610, 80, black)
    Draw.Line (610, 80, 560, 30, black)
    Draw.Line (560, 80, 610, 30, black)
    Draw.Fill (561, 32, 40, black)
    Draw.Fill (565, 32, 40, black)
    Draw.Fill (591, 71, 40, black)
    Draw.Fill (601, 61, 40, black)

    %Draws Master Power Switch
    Draw.ThickLine (610, 130, 560, 130, 3, black)
    Draw.ThickLine (610, 130, 610, 230, 3, black)
    Draw.ThickLine (610, 230, 560, 230, 3, black)
    Draw.ThickLine (560, 130, 560, 230, 3, black)
    Draw.Fill (571, 201, yellow, black)

    %Clears and resets the screen every time it loops
    View.Update
    cls
delay (100)

    if x >= 560 and x <= 610 and y >= 30 and y <= 80 and button = 1 then     %if the mouse is clicked within the red box w/ an "x" then the program will quit
        quit
    end if


    %grid
end loop



alright here is the final code all i did to fix the flickering was add a delay at the end of the loop.

Thanks for all of your help guys Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: