OspreyGozo

Mechanical 7 Segment Display Sample

Sep 9th, 2019
234
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_PWMServoDriver.h>
  3.  
  4. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  5.  
  6. #define MIN_PULSE_WIDTH0 610
  7. #define MAX_PULSE_WIDTH0 2325
  8. #define DEFAULT_PULSE_WIDTH 1500
  9. #define FREQUENCY 50
  10.  
  11. #define MIN_ANGLE 0
  12. #define MAX_ANGLE 90
  13.  
  14. int iCounter = 0;
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(115200);
  19.   Serial.println("16 channel Servo test!");
  20.  
  21.   pwm.begin();
  22.   pwm.setPWMFreq(FREQUENCY);
  23.  
  24.   // Reset all segments of all digits //
  25.   for(int DigitNum = 0; DigitNum <= 6; DigitNum++){
  26.     for(int ServoNum = 0; ServoNum <= 6; ServoNum++){
  27.       pwm.setPWM(ServoNum, DigitNum, pulseWidth(MIN_ANGLE, MIN_PULSE_WIDTH0, MAX_PULSE_WIDTH0));
  28.     }
  29.   }
  30.   //////////////////////////////////////
  31.  
  32.   delay(2000);
  33. }
  34.  
  35. int pulseWidth(int angle, int MIN_PULSE_WIDTH, int MAX_PULSE_WIDTH)
  36. {
  37.   int pulse_wide, analog_value;
  38.   pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  39.   analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
  40.   return analog_value;
  41. }
  42.  
  43. void SetSegment(int ServoNum, int DigitNum, boolean bState)
  44. {
  45.   if(!bState){
  46.     pwm.setPWM(ServoNum, DigitNum, pulseWidth(MIN_ANGLE, MIN_PULSE_WIDTH0, MAX_PULSE_WIDTH0));
  47.   }
  48.   else {
  49.     pwm.setPWM(ServoNum, DigitNum, pulseWidth(MAX_ANGLE, MIN_PULSE_WIDTH0, MAX_PULSE_WIDTH0));
  50.   }
  51. }
  52.  
  53. void SetDigit(int DigitNum, int DigitValue) {
  54.   Serial.println(String(DigitValue));
  55.   switch (DigitValue) {
  56.     case 0:
  57.       SetSegment(0, DigitNum, 1);
  58.       SetSegment(1, DigitNum, 1);
  59.       SetSegment(2, DigitNum, 1);
  60.       SetSegment(3, DigitNum, 1);
  61.       SetSegment(4, DigitNum, 1);
  62.       SetSegment(5, DigitNum, 1);
  63.       SetSegment(6, DigitNum, 0);
  64.       break;
  65.     case 1:
  66.       SetSegment(0, DigitNum, 0);
  67.       SetSegment(1, DigitNum, 1);
  68.       SetSegment(2, DigitNum, 1);
  69.       SetSegment(3, DigitNum, 0);
  70.       SetSegment(4, DigitNum, 0);
  71.       SetSegment(5, DigitNum, 0);
  72.       SetSegment(6, DigitNum, 0);
  73.       break;
  74.     case 2:
  75.       SetSegment(0, DigitNum, 1);
  76.       SetSegment(1, DigitNum, 1);
  77.       SetSegment(2, DigitNum, 0);
  78.       SetSegment(3, DigitNum, 1);
  79.       SetSegment(4, DigitNum, 1);
  80.       SetSegment(5, DigitNum, 0);
  81.       SetSegment(6, DigitNum, 1);
  82.       break;
  83.     case 3:
  84.       SetSegment(0, DigitNum, 1);
  85.       SetSegment(1, DigitNum, 1);
  86.       SetSegment(2, DigitNum, 1);
  87.       SetSegment(3, DigitNum, 1);
  88.       SetSegment(4, DigitNum, 0);
  89.       SetSegment(5, DigitNum, 0);
  90.       SetSegment(6, DigitNum, 1);
  91.       break;
  92.     case 4:
  93.       SetSegment(0, DigitNum, 0);
  94.       SetSegment(1, DigitNum, 1);
  95.       SetSegment(2, DigitNum, 1);
  96.       SetSegment(3, DigitNum, 0);
  97.       SetSegment(4, DigitNum, 0);
  98.       SetSegment(5, DigitNum, 1);
  99.       SetSegment(6, DigitNum, 1);
  100.       break;
  101.     case 5:
  102.       SetSegment(0, DigitNum, 1);
  103.       SetSegment(1, DigitNum, 0);
  104.       SetSegment(2, DigitNum, 1);
  105.       SetSegment(3, DigitNum, 1);
  106.       SetSegment(4, DigitNum, 0);
  107.       SetSegment(5, DigitNum, 1);
  108.       SetSegment(6, DigitNum, 1);
  109.       break;
  110.     case 6:
  111.       SetSegment(0, DigitNum, 1);
  112.       SetSegment(1, DigitNum, 0);
  113.       SetSegment(2, DigitNum, 1);
  114.       SetSegment(3, DigitNum, 1);
  115.       SetSegment(4, DigitNum, 1);
  116.       SetSegment(5, DigitNum, 1);
  117.       SetSegment(6, DigitNum, 1);
  118.       break;
  119.     case 7:
  120.       SetSegment(0, DigitNum, 1);
  121.       SetSegment(1, DigitNum, 1);
  122.       SetSegment(2, DigitNum, 1);
  123.       SetSegment(3, DigitNum, 0);
  124.       SetSegment(4, DigitNum, 0);
  125.       SetSegment(5, DigitNum, 0);
  126.       SetSegment(6, DigitNum, 0);
  127.       break;
  128.     case 8:
  129.       SetSegment(0, DigitNum, 1);
  130.       SetSegment(1, DigitNum, 1);
  131.       SetSegment(2, DigitNum, 1);
  132.       SetSegment(3, DigitNum, 1);
  133.       SetSegment(4, DigitNum, 1);
  134.       SetSegment(5, DigitNum, 1);
  135.       SetSegment(6, DigitNum, 1);
  136.       break;
  137.     case 9:
  138.       SetSegment(0, DigitNum, 1);
  139.       SetSegment(1, DigitNum, 1);
  140.       SetSegment(2, DigitNum, 1);
  141.       SetSegment(3, DigitNum, 1);
  142.       SetSegment(4, DigitNum, 0);
  143.       SetSegment(5, DigitNum, 1);
  144.       SetSegment(6, DigitNum, 1);
  145.       break;
  146.     default:
  147.       SetSegment(0, DigitNum, 0);
  148.       SetSegment(1, DigitNum, 0);
  149.       SetSegment(2, DigitNum, 0);
  150.       SetSegment(3, DigitNum, 0);
  151.       SetSegment(4, DigitNum, 0);
  152.       SetSegment(5, DigitNum, 0);
  153.       SetSegment(6, DigitNum, 0);
  154.       break;
  155.   }  
  156. }
  157.  
  158. void loop() {
  159.   SetDigit(0,iCounter);
  160.   delay(1000);
  161.   iCounter++;
  162.   if(iCounter>=10){
  163.     iCounter = 0;
  164.   }
  165. }
RAW Paste Data