// POV Display - complete code // (Worksheet 4.5.1 - Pin Maps, Initialization) // (Worksheet 4.5.1 Number 4.) // The const keyword is a variable qualifier that tells the compiler that we don't want this variable's value to change // It is a great way to define things like pins and colors. // Here we define all the i/o pins by name; once, and only once. // // color pins: 2 pins per color, both have to be on. const int red1 = 8; const int red2 = 9; const int grn1 = 10; const int grn2 = 11; const int blu1 = 12; const int blu2 = 13; // // row pins const int row0 = 0; const int row1 = 1; const int row2 = 2; const int row3 = 3; const int row4 = 4; const int row5 = 5; const int row6 = 6; const int row7 = 7; // magneticSensor pin const int magSensor = A0; // (Worksheet 4.5.1 Number 5.) // Add 3 lines of code to define aliases for red, blue, and green. // These constants help us navigate through the image file const int red = 0; const int green = 1; const int blue = 2; // putting the color and row pins in arrays lets us reference them with loop counters // defining a variable outside of the program loops makes it global so every function in this file can reference it. // (Worksheet 4.5.1 Number 6.) // Define a constant array for the 8 row pins. const int rowPin[8] = { row0, row1, row2, row3, row4, row5, row6, row7 }; // (Worksheet 4.5.1 Number 7.) // Define an array that contains the first Red, Green, and Blue color pins. int colorPin[3] = { red1, grn1, blu1 }; // (Worksheet 4.5.1 Number 7.) // The second row of color pins is necessary because each pin can only drive four LEDs at a time at 10mA each. // Define another array called colorPin2 that contains the second set of Red, Green, and Blue // color pins using the above array as an example. int colorPin2[3] = { red2, grn2, blu2 }; // To turn on an LED, we have to set the Color pin HIGH and the row pin LOW. // Only one Color pin should be on at a time. // To avoid driving the LED backwards we can't drive the row pins HIGH, we have to turn them off by // setting them up as INPUTS // (Worksheet 4.5.1 Number 8.) // Write a function here that will turn all 6 color pins off by using the “digitalWrite” // command and setting the color pins LOW. You have been given the initial function // definition statement and one of the necessary “digitalWrite” statements. // You need to fill in the rest of the statements that the function will // do when it is called. // void clearColorPins(void) { // Turn all color drivers off digitalWrite(red1, LOW); digitalWrite(red2, LOW); digitalWrite(blu1, LOW); digitalWrite(blu2, LOW); digitalWrite(grn1, LOW); digitalWrite(grn2, LOW); } // Here is a function that will turn OFF only one pair of color pins. // This routine could be used instead of clearColorPins if we are careful to keep track of // which one is on. void clearColorPin(int color) { digitalWrite(colorPin[color], LOW); digitalWrite(colorPin2[color], LOW); } // (Worksheet 4.5.1 Number 9.) // Write a function called setColorPin() that will turn ON one pair of color pins. void setColorPin(int color) { // make sure only one color is on at a time - turn everything off first then turn the one we want on clearColorPins(); digitalWrite(colorPin[color], HIGH); digitalWrite(colorPin2[color], HIGH); } // (Worksheet 4.5.1 Number 10.) // Setting the pin mode to OUTPUT turns the LED on, setting pin mode to INPUT turns the // LED off. The value should always be LOW. // Write a function called setRowPin(int row) to set (turn on) a row pin. void setRowPin(int row) { pinMode(rowPin[row], OUTPUT); } // Write a function called clearRowPin(int row) to clear (turn off) a row pin. (Worksheet 4.5.1 Number 10.) void clearRowPin(int row) { pinMode(rowPin[row], INPUT); } // (Worksheet 4.6.1s Number 3) // Create a function called clearRowPins() which uses a for() loop to turn OFF all eight of the LED row pins // Use the functions you've already created to set and clear individual row pins void clearRowPins(void) { for (int i = 0; i < 8; i++) { clearRowPin(i); } } // (Worksheet 4.6.1s Number 4) // Create another function called setRowPins() which uses a for() loop to turn ON all eight of the LED row pins // It will look very similar to the clearRowPins function. // void setRowPins(void) { for (int i = 0; i < 8; i++) { setRowPin(i); } } // (Worksheet 4.8.1s Number 4) // Copy the graphics file supplied by your teammembers to the same directory as your code file // This line should go somewhere near the top of your file. // Edit it to exactly match the name of the file as it appears on the tab. The quote marks are necessary. // Make sure that their are no spaces or special characters in the filename - the code won't work if there are. // This line brings in the graphics file #include "PacMan.c" // (Worksheet 4.8.1s Number 5) // Create a function to return the value of a pixel from gimp_image given a row, column and color. // Use width and bytes_per_pixel as stored in the struct so the function will work even if these parameters change. // This function should return an integer (int, not void). Check that the name of the struct variable matches // the code in your graphics file. // This function returns the intensity value for the pixel at row, column and color // It only works with 24-bit rgb color files int getPixel(int row, int column, int color) { int index = (row * gimp_image.width + column ) * gimp_image.bytes_per_pixel; return gimp_image.pixel_data[index + color]; } // (Worksheet 4.9.1s Number 5) // To display the image in the same place each time we need to have a reference point in the rotation. // We will use a Hall-effect sensor that toggles between HIGH and LOW each time the handle turns. // First wait for the sensor to be HIGH, then wait for the sensor to go LOW again. // The sensor is connected to an analog pin which has a range from 0 to 1023 (10 bits). // By setting a HIGH threshold of 600 and a LOW threshold of 400 we avoid false triggers as the input slowly moves through the LOW to HIGH transition. This is called hysteresis. // Create a function called void waitForTrigger() that reads the analog I/O pin connected to the Hall-effect sensor. // Functions should be created outside of any other function definitions. // Use analogRead() to get the input value. You should have defined a reference to the pin earlier // Use a first while () loop to wait until the pin is high (value > 600). // Then use a second while() loop to wait until it goes low (<400). void waitForTrigger(void) { while (analogRead(magSensor) < 600) { // stay here as long as the pin value is less LOW (less than 600). } while ( analogRead(magSensor) > 400 ) { // just keep checking until it goes low again } } // Everything above this point is a definition. // setup() is where things actually start to run. void setup() { // put your setup code here, to run once: // (Worksheet 4.5.1 Number 11.) // The color pins will be driven HIGH to turn them on, LOW to turn them off // for starters we turn them all off and set them as outputs clearColorPins(); // We have a function to set them all low // Now set the pinModes - I've given you one, do the rest pinMode(red1, OUTPUT); pinMode(red2, OUTPUT); pinMode(blu1, OUTPUT); pinMode(blu2, OUTPUT); pinMode(grn1, OUTPUT); pinMode(grn2, OUTPUT); // (Worksheet 4.5.1 Number 11.) // The row pins will always be LOW // We turn them on and off by changing their pinMode // Add code to set the value LOW and pinMode to INPUT for each row pin (Worksheet 4.5.1 Number 11.) digitalWrite(row0, LOW); digitalWrite(row1, LOW); digitalWrite(row2, LOW); digitalWrite(row3, LOW); digitalWrite(row4, LOW); digitalWrite(row5, LOW); digitalWrite(row6, LOW); digitalWrite(row7, LOW); pinMode(row0, INPUT); // need 7 more: is there an easier way to do this? pinMode(row1, INPUT); pinMode(row2, INPUT); pinMode(row3, INPUT); pinMode(row4, INPUT); pinMode(row5, INPUT); pinMode(row6, INPUT); pinMode(row7, INPUT); /* here is another way to do the same thing for (int i=0; i< 8; i++ ) { digitalWrite(rowPin{i], LOW); pintMode(rowPin[i], INPUT); } */ // (Worksheet 4.5.1 Number 11.) // Write the necessary statements to set the magnetic sensor as an INPUT with a value of HIGH. pinMode(magSensor, INPUT); digitalWrite(magSensor, HIGH); // (Worksheet 4.6.1s Number 5) // In the setup() function after all the initialization code create a for loop that turns on each color // (red, green and blue or 0, 1, 2) in turn and then waits 1 second ( delay(1000); ) // Before the for loop call setRowPins(); after it call clearRowPins(). // setRowPins(); for (int color = 0; color < 3; color++ ) { setColorPin(color); delay(1000); } clearRowPins(); // (Worksheet 4.6.1s Number 8) // Create a set of three nested for loops. // The outermost loop should cycle through each of the eight row pins in turn, // turning on that row at the beginning, and turning it off at the end. // The next inner loop (between the on and off commands) should count to 128. // The inner most loop should cycle between the three colors setting each and then // waiting for a short time (1 or 2 mSec) for (int row = 0; row < 8; row++) { setRowPin(row); for (int i = 0; i < 128; i++) { for (int color = 0 ; color < 3; color++ ) { setColorPin(color); } } clearRowPin(row); } } void loop() { // (Worksheet 4.9.1s Number 6) // Call the waitForTrigger() function at the beginning of your main loop() function. waitForTrigger(); // (Worksheet 4.8.1s Number 6) // Create a for loop that cycles through each of the columns in the picture (gimp_image.width) for (int column = 0; column < 8 ; column++) { // (Worksheet 4.8.1s Number 7) // Inside the column for loop create another for loop which cycles through the 3 colors (r, g and b). // Turn the appropriate color pins on inside this loop for ( int color = 0; color < 3; color++) { // (Worksheet 4.8.1s Number 8) // Inside the color loop create a loop which counts from 0 to 255. // This loop will adjust the intensity. // If a pixel value is greater than the loop counter we turn it ON, // otherwise we turn it off. A pixel with a value of 127 will be on for half of the time. for ( int i = 0; i < 256; i += 8) { // (Worksheet 4.8.1s Number 9) // Create one final loop which cycles through the eight rows. // In this loop use getPixel(row, column, color) // and an if() and else command to check if we should turn the pixel on or off. for (int row = 0; row < 8; row += 1) { if (getPixel(row, column, color) > i) setRowPin(row); // the pixel will be on for length of time porportional to the desired brightness else clearRowPin(row); } } } } // (Worksheet 4.8.1s Number 10) // Clear the row and column pins after the loops have executed so that we don’t leave anything on. clearColorPins(); // make sure nothing is left on while we wait clearRowPins(); }