Friday, 18 March 2011

Turning Flowcharts into Programs

A flowchart is the planning stage for a program.  It is important to use the correct shaped box so that it is clear when you are controlling an output, or asking a question etc.  The flowchart is to help you understand the sequence so it is written in English.

The first flowchart is to practice asking about the condition of an input:


First you must choose which pins you are going to use as outputs and which as inputs.  Looking at the board you know that pin 7 is a red LED and 5 is a green LED. I'm going to use pin 0 as my switch.

So I need to tell the microprocessor this:

init: let dirs = %10100000     'Sets pin 7 and 5 as outputs
       symbol green = 5
       symbol red = 7

Now I can write my main program.  If I want to use English words, I must first of all tell the microprocessor this as well by setting the English words as symbols.  If you prefer not to do this, use the black commands, if you would like to do this, follow the purple programming language.

To ask a question I need to use an IF . . . THEN . . .  statement in relation to my input pin.  Then I need to tell the microprocessor which bit of program to go to when my input is on, and when it is not.  If the statement is not true, it will not go to the label which follows THEN but to the next line instead.

main: if pin0 = 1 then green_on       'Check the input pin, if it is on, go to the
                                                                    'green_on sub-procedure.
          goto red_on                            'If the input pin is off, go to the
                                                                    'red_on sub-procedure

red_on: high 7           high red          'Switch on the red LED
             low 5            low green        'Switch off the green LED
             goto main                             'Loop back to the start to test the input

green_on: high 5        high green      'Switch the green LED on
                 low 7         low red          'Switch the red LED off
                 goto main                        'Loop back to the start to test the input




The second flowchart incorporates testing an input and using a counter loop.  To repeat something a certain number of times, you must use a temporary memory file in the RAM to store the number of times the loop has been repeated.  Often we will use b0 for this purpose.  Again, if you want to call b0 something in English you must use the symbol command.

The PBASIC language required to repeat a sequence is a FOR . . . NEXT loop.  For must go at the top of the sequence and next at the bottom.  Anything you write in between FOR and NEXT will be repeated.

Motors, as described earlier in the blog, must be connected using the push-pull driver on the output driver.  7&6 control one motor, and 4&5 control another motor if you require it.  It is important to use the correct pairing to ensure that your motor will turn.

The program for this flowchart would therefore be:

init: let dirs = %11000000             'sets 7 and 6 as outputs
       symbol counter = b0

main: if pin0 = 0 then main     OR    main: if pin0 = 1 then motor   'both of these statements check
                                                                    goto main                       the input.
motor: for counter = 1 to 5           'Starts the loop to repeat 5 times
              high 7                              'motor clockwise on
              pause 10000                    'wait 10 seconds
              low 7                               'motor clockwise off
              high 6                              'motor anticlockwise on
              pause 10000                    'wait 10 seconds
              low 6                               'motor anticlockwise off
          next counter                        'If it has not been repeated 5 times, loop back to motor
          end



The third flowchart is in two parts.  This shows that the sequence uses a sub-procedure.  Where "alarm" is in the main flowchart, it means that the whole of the alarm flowchart fits into that box.  So from the main flowchart, the sequence goes to the start of alarm, and at the end of alarm it returns to the main program and follows the arrow back round to the start.

Here is the program:

init: let dirs = %10000000          'sets pin 7 as the output (buzzer)

main: if pin0 = 0 then alarm        'if the door is opened go to the sub-procedure alarm
          if pin1 = 0 then alarm        'if the window is opened go to the sub-procedure alarm
          if pin2 = 0 then alarm        'if the door mat is stepped on go to the sub-procedure alarm
          goto main                           'go back to the start and test the inputs again.

alarm: high 7                               'buzzer on
           pause 500                         'wait half a second
           low 7                                'buzzer off
           pause 500                         'wait half a second
           if pin3 = 0 then alarm      'test the reset switch, if it is not pressed then continue sounding the alarm
           return   /   goto main        'return to the main program.

Your stepper motor program needed to turn the motor 60 degrees clockwise when a switch is pressed:

symbol counter = b1
symbol delay = b0
let delay = 40     (BECAUSE you need to take 320ms to repeat 8 steps of the stepper motor)



main: if pin0 = 0 then main          'tests the input

clock: for counter = 1 to 2            'start loop to repeat twice as each sequence of 4 steps is 30
               let pins = %10100000
               pause delay
               let pins = %10010000
               pause delay
               let pins = %01010000
               pause delay
               let pins = %01100000
               pause delay
           next counter                       'repeat again
           goto main

Stepper Motors

Stepper motors are motors which turn due electromagnetic coils pulling the axle round in a circle.  There is an animation of this on this website:

Stepper Motor

This is useful for two reasons: you can make the stepper motor turn to a particular position and it has a "holding torque" meaning that as long as two of the magnets are energised it can hold something in position.

To make the stepper motor turn you must use a particular pattern to make sure that the coils energise in a circle.  To make the stepper motor turn clockwise you type the pattern in as seen, to make it turn anticlockwise you simply use the same pattern but in reverse:

i.e.

symbol delay = b0
let delay = 100

clock: let pins = %10100000
           pause delay
           let pins = %10010000
           pause delay
           let pins = %01010000
           pause delay
           let pins = %01100000
           pause delay
           goto clock

anti:    let pins = %01100000
           pause delay
           let pins = %01010000
           pause delay
           let pins = %10010000
           pause delay
           let pins = %10100000
           pause delay
           goto anti

Repeating like this will simply make the stepper motor turn continuously.   By using b0 as the delay, we can quickly change the speed of the stepper motor by changing the value of "delay"  The smaller the number the faster the motor will turn, but if you try to turn too quickly the magnets will not have time to pull the axle round so it may not turn at all.

To make the motor turn to a specific position, we must use a FOR . . . NEXT . . . loop.  This will allow us to repeat the pattern a set number of times.  Each line of the program is 7.5 degrees, so each 4 line pattern is 30 degrees.  Therefore to turn one complete revolution we must repeat the pattern 12 times.

i.e.

symbol counter = b0
symbol delay = b1
let delay = 100

clock: for counter = 1 to 12
                  let pins = %10100000
                  pause delay
                  let pins = %10010000
                  pause delay
                  let pins = %01010000
                  pause delay
                  let pins = %01100000
                  pause delay
            next counter


The stepper motor requires a 12v supply and the microprocessor can only supply a 5v signal.  Therefore we must use an external power supply (a black box) to connect to our output driver.

Thursday, 17 March 2011

NEW feature! Reactions!

I have added some "reactions" to the blog.  This allows you to quickly comment on how much you understand something.  Please use this so that I can gauge how much people understand, and which areas I need to go over more.

You don't have to comment, and it's anonymous so please give me this feedback so that I can tailor my teaching to help you.  If you would like me to go over something individually then leave a comment and I'll catch up with you :)

Friday, 11 March 2011

Today's Mini Test

Here is today's mini test which will help us prepare for the NAB.  You also have to be able to describe the three types of control: On/Off, Continuous and Sequential in relation to a given example.

Wednesday, 9 March 2011

Mechanisms Practice Question.

For this (sketched) situation,

1. Draw the system diagram

2. Calculate the linear speed of the load.

Monday, 7 March 2011

Moments Question Answer

Here is how to go about the moments question.

First a line drawing should be drawn.  First draw the line.  All forces should now act onto that line:






Now you can do the maths do find the values of Ra and Rb.

Wednesday, 2 March 2011

Practice Moments Questions

To aid your revision of moments, you can try this practice question.  If you e-mail me your answers, I'll e-mail you my working so you can see how I went about it :)