Skip to content

Commit

Permalink
Merge pull request #21 from rharbird/new_pxt
Browse files Browse the repository at this point in the history
Add sine material
  • Loading branch information
rharbird committed Sep 7, 2019
2 parents a908e58 + cedff26 commit 4caee92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Binary file added docs/source/pictures/sineMotors.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions docs/source/sine.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
***********************
Doing it as a sine wave
***********************
This is an extension activity.

As it happens, many things in biology happen like sine waves. That's true for this kind of movement - what we'd like to see is the following:
.. image:: pictures/sineMotors.png
:scale: 50%

What this means is the change in angle is slower the closer you get to 180 and to 0. So it starts slowly, then gets faster, then slows down, changes direction, and so on.

The function for the angle is:

angle = 90 + 90 * Math.sin(2 * PI * running_time / 4000)

Note: the Math.sin() function requires the number of degrees expressed as radians. 360° is 2 * PI radians.

* ``running_time`` is the amount of time since the micro:bit was turned on in ms.
* The 4000ms is the amount of time it takes to go from angle 0° back to 0° again. Make this number bigger to go slower.
* Sine values vary between 0 and 1 so the angle varies between 0° and 180° as things stand. The __90 *__ is the amplitude of the wave and here we want it to go through all 180° of motion. Make this smaller if you want less; if the value was, say 40, then the movement would vary between 60° and 130°.
* The __90 +__ is the centre position position of the movement. The servo moves equally either side of that centre. You could choose to centre the movement in a different place. If this was say, 100 and the amplitude was 40 then the servo would move between 60° and 140°.
Congratulations! You made an oscillator. The nice thing about sinusoidal oscillators is that putting things out of phase is easy. Say you wanted two sine waves 90° out of phase then set:``angle1`` = sin(t) and ``angle2`` = sin(t + 90)

0 comments on commit 4caee92

Please sign in to comment.