1 write a function that takes a frame number and

This preview shows page 9 - 11 out of 12 pages.

1.) Write a function, that takes a frame number and calls writeColor() as described above: void drawFrame( unsigned char frame); 2.) Add drawFrame() to loop() wrapping it with code that increments the frame from 0 to 255, then decrements the frame from 255 to 0. (i.e. the iLED should follow the transition Red -> Purple -> Blue -> Purple -> Red and repeat.). Follow each call of drawFrame() by a delay of 1ms. 3.) When your code compiles successfully, load it on your PIC24. Due to the difference between human and machine perception rates, it is useful to control the rate of an animation with a compiler constant. 4.) Write a new C function that calls your ASM function wait_1ms() a variable number of times (this doesn’t have to exact N milliseconds, a minimal amount of overhead is ok): void delay(int delay_in_ms) 5.) Create a compiler constant: #define PERIOD 5 (where the period of blinks is PERIOD, in milliseconds) 6.) Utilize the above constant and the new delay function to make your program easily change frame rate. 7.) Modify your frame rate to that specified by your TA and demonstrate your program. Adafruit Wheel() function NOTE : This section is less guided than your previous lab procedures. Expect to get stuck occasionally. When you get stuck, consult with your neighbors, talk to your TA, ask questions. If you’re working outside the lab, ask questions on the forum. In this section you will translate an existing Adafruit Library function into code compatible with your newly built functions. The Adafruit NeoPixel Library has a neat function buried in their example code. The Wheel() function cycles through the 3-dimensional RGB color space, with a 1-dimensional frame number input. The is useful for testing all three LEDs inside the iLED. It also looks kinda cool. The Adafruit NeoPixel Library → // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(unsigned char WheelPos) { WheelPos = 255 - WheelPos; Page 9
EE 2361 - Lab # 2b ECE Department if(WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } Excerpt from “strandtest.ino” from the Adafruit NeoPixel Library The first thing to notice is that Adafruit NeoPixel library operates on 32-bit “packed color” integers. These are simply a convenient way of moving 3-variable data around. They are formatted as 0x00RRGGBB. The Microchip compiler does not inherently support the datatype syntax “uint32_t”. This syntax is a compiler agnostic method of specifying data types. It is synonymous with the Microchip data type “unsigned long int”. If you want to use compiler agnostic methods just include “#include "stdint.h"” at the top of your main C file. The second thing to notice is that Adafruit uses a datatype called “byte” (again not supported by default by Microchip’s compiler XC16). A byte is an “ 8-bit unsigned two’s complement number ”. For our purposes just replace “byte” with “unsigned char”. Packing and unpacking variables is actually very easy and can accomplished in several ways, here are some hints: unsigned char Red = 0x40;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture