Advertisement
Guest User

Untitled

a guest
Feb 4th, 2021
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. //Program to shuffle the names of students in a class and display a different student name
  2. //every time a button is pressed. Display is a 4 x 8x8 Dot matrix panel controlled by MAX7219
  3. #include <Arduino.h>
  4. #include <MD_Parola.h>  //Library of nice functions for the display
  5. #include <MD_MAX72xx.h> //Driver for display
  6. #include <SPI.h>
  7. #include "LowPower.h"
  8. #define NEXT_PIN 2 //Pin to get next student
  9. #define CLASS_PIN 3 //pin for the pushbutton to change classes
  10. #define CS_PIN 4  //control pin for the display
  11. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
  12. #define MAX_DEVICES 4  //Number of 8x8 LED matrices
  13. #define DISPLAY_SCROLL_SPEED  50 //msec between frames
  14. #define DISPLAY_SCROLL_PAUSE_TIME  0
  15. MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); //new instance of MD_Parola class with hardware SPI
  16.  
  17. //Edit these variables to match your situation.
  18. const int maxClassSize = 9;  //Enter the number of students in the largest class
  19. const int numberOfClasses = 3; //How many classes
  20. const int numberOfStudents[] = {9,8, 8}; //Enter the number of students in each class
  21. const char *classNames[] = {"Geo", "Calc C", "Calc D"}; //Abbreviate to < 8 characters to fit on display
  22. const char *classRosters[numberOfClasses][maxClassSize] = {
  23.             {"Sam", "Anna", "Rachel", "Bella", "Zach", "Trinity", "Billy", "Jose",
  24.               "Cait"},
  25.             {"Ed", "Francis", "Gary", "Hal", "Mel", "Curt", "Doug", "Emma"},
  26.             {"Lucy", "Victoria", "Nell", "Pauline", "Helena", "Rory", "Sam", "Lucy"},
  27.           };
  28.  
  29. void shuffleStudents(); //declare functions
  30. void changeClass();
  31. void getNextStudent();
  32. void wakeUp();
  33.  
  34. int shuffledIndexes[maxClassSize];
  35. int thisClass = numberOfClasses - 1;
  36. int thisStudent = 0;
  37. int nextPinUp = false;
  38. int classPinUp = false;
  39.  
  40. void setup() {
  41.   delay(500);
  42.   pinMode(NEXT_PIN, INPUT_PULLUP); //The pushbutton pin will have an internal resistor pulling the value high when not depressed
  43.   pinMode(CLASS_PIN, INPUT_PULLUP);
  44.   pinMode(CS_PIN, OUTPUT);
  45.  
  46.   randomSeed(analogRead(0)); //get noise from pin 0 as seed for random number generator
  47.  
  48.   myDisplay.begin();  //Start the display module
  49.   myDisplay.setIntensity(7);  // Set the intensity (brightness) of the display (0-15):
  50.   myDisplay.setTextAlignment(PA_CENTER);
  51.   myDisplay.displayClear();
  52.   myDisplay.displayReset();
  53.   changeClass(); //go to the next class and display its name.
  54. }
  55.  
  56. void loop() {
  57.   //go into low power mode and wait for a button to be pushed.
  58.   attachInterrupt(digitalPinToInterrupt(NEXT_PIN), wakeUp, LOW); //enable interrupts
  59.   attachInterrupt(digitalPinToInterrupt(CLASS_PIN), wakeUp, LOW);
  60.   LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  61.   detachInterrupt(digitalPinToInterrupt(CLASS_PIN));  //disable interrupts until after you deal with the first.
  62.   detachInterrupt(digitalPinToInterrupt(NEXT_PIN));
  63.   do { //read both buttons to see which has been pushed
  64.     classPinUp = digitalRead(CLASS_PIN);
  65.     nextPinUp = digitalRead(NEXT_PIN);
  66.   } while (classPinUp and nextPinUp);
  67.  
  68.   if (!classPinUp) {
  69.     changeClass();
  70.   } else {
  71.     getNextStudent();
  72.   }
  73. }
  74.  
  75. void changeClass() {
  76.   thisClass = (thisClass + 1) % numberOfClasses;
  77.   shuffleStudents();
  78.   myDisplay.displayShutdown(false);
  79.   myDisplay.print(classNames[thisClass]);
  80.   delay(800);
  81.   myDisplay.displayClear();
  82.   myDisplay.displayShutdown(true); //put into low power mode
  83. }
  84.  
  85. void shuffleStudents() {
  86.   for (int i = 0; i < numberOfStudents[thisClass]; i++){
  87.     shuffledIndexes[i] = i;
  88.   }
  89.   for (int i = 0; i < numberOfStudents[thisClass]; i++) {
  90.     int j = random(0, numberOfStudents[thisClass]);
  91.     int t = shuffledIndexes[i];
  92.     shuffledIndexes[i] = shuffledIndexes[j];
  93.     shuffledIndexes[j] = t;
  94.   }
  95. }  
  96.  
  97. void getNextStudent() {
  98.   myDisplay.displayShutdown(false); //wake up display from low power mode
  99.   thisStudent = (thisStudent + 1) % numberOfStudents[thisClass];
  100.   int del = 0;
  101.   for (int i = 0; i < 30; i++) {
  102.     int student = (thisStudent + i) % numberOfStudents[thisClass];
  103.     myDisplay.print(classRosters[thisClass][student]);
  104.     del = del + i/4;
  105.     delay(del);
  106.   }  
  107.     myDisplay.setInvert(true);
  108.     myDisplay.print(classRosters[thisClass][shuffledIndexes[thisStudent]]);
  109.     delay(200);
  110.     myDisplay.setInvert(false);
  111.     if (myDisplay.getTextColumns(classRosters[thisClass][shuffledIndexes[thisStudent]]) > MAX_DEVICES * 8) {
  112.       //If the name it too long to fit, scroll the name
  113.       myDisplay.displayClear();
  114.       myDisplay.displayText(classRosters[thisClass][shuffledIndexes[thisStudent]], PA_CENTER, DISPLAY_SCROLL_SPEED, DISPLAY_SCROLL_PAUSE_TIME, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
  115.       while(true) { //wait for animation to finish
  116.         if (myDisplay.displayAnimate()) {
  117.           myDisplay.displayReset();
  118.           break;
  119.         }
  120.       }
  121.     } else {
  122.       myDisplay.print(classRosters[thisClass][shuffledIndexes[thisStudent]]);
  123.       delay(4000);
  124.     }
  125.     myDisplay.displayClear();
  126.     myDisplay.displayShutdown(true); //put into low-power mode
  127. }
  128.  
  129. void wakeUp(){
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement