Advertisement
andrewb

_7seg_test.cpp

Oct 8th, 2014
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. // For later
  2. const int W_H = 1;
  3. const int W_L = 0;
  4. const int M_O = 1;
  5. const int M_I = 0;
  6. // MAX PINS
  7. const int MAX_PINS = 12;
  8. // Hold pin info
  9. struct {
  10.   int m;
  11.   int w;
  12.   int number;
  13. } pins[12];
  14. // Characters for pin commands
  15. char pin_number = 'x';
  16. char pin_action = 'x';
  17. char pin_chars[MAX_PINS] = {'2','3','4','5','6','7','8','9','A','B','C','D'};
  18.  
  19. void setup() {
  20.   // Initialize pins and struct
  21.   for (int pc=0; pc < MAX_PINS; pc++) {
  22.     int temp_pin = pc + 2;
  23.     pinMode(temp_pin, INPUT);
  24.     pins[pc].m = M_I;
  25.     pins[pc].w = W_L;
  26.     pins[pc].number = temp_pin;
  27.   }
  28.  
  29.   // Start serial
  30.   Serial.begin(9600);
  31.   while(!Serial) { ; }
  32. }
  33.  
  34. void loop() {
  35.   // Check for incoming bits that are looking for the controller
  36.   if (Serial.available() > 0) {
  37.     char c = Serial.read();
  38.     if (pin_number == 'x') {
  39.       pin_number = c;
  40.     }
  41.     else if (pin_action == 'x') {
  42.       pin_action = c;
  43.     }
  44.   }
  45.   // If you have enough, go
  46.   if (pin_number != 'x' && pin_action != 'x') {
  47.     goCommand();
  48.   }
  49.   // Pause
  50.   delay(100);
  51. }
  52.  
  53. int findPin(int pin) {
  54.   for(int cnt=0; cnt < MAX_PINS; cnt++) {
  55.     if (pin == pins[cnt].number) {
  56.       return cnt;
  57.     }
  58.   }
  59.   return 99;
  60. }
  61.  
  62. int findPinFromChar() {
  63.   for (int cnt = 0; cnt < MAX_PINS; cnt++) {
  64.     if (pin_chars[cnt] == pin_number) {
  65.       return cnt;
  66.     }
  67.   }
  68.   return 99;
  69. }
  70.  
  71. void switchMode(int pin) {
  72.   int c = findPin(pin);
  73.   if (c >= MAX_PINS) { return; }
  74.   if (pins[c].m == M_O) {
  75.     pinMode(pins[c].number, INPUT);
  76.     pins[c].m = M_I;
  77.   } else {
  78.     pinMode(pins[c].number, OUTPUT);
  79.     pins[c].m = M_O;
  80.   }
  81. }
  82.  
  83. void switchWrite(int pin) {
  84.   int c = findPin(pin);
  85.   if (c >= MAX_PINS) { return; }
  86.   if (pins[c].m == M_I) { return; }
  87.   if (pins[c].w == W_H) {
  88.     digitalWrite(pins[c].number, LOW);
  89.     pins[c].w = W_L;
  90.   } else {
  91.     digitalWrite(pins[c].number, HIGH);
  92.     pins[c].w = W_H;
  93.   }
  94. }
  95.  
  96. void feedBack() {
  97.   int p = findPinFromChar();
  98.   if (p == 99) return;
  99.   int pmw = pins[p].m + (pins[p].w * 2);
  100.   Serial.print(pin_chars[p]);
  101.   Serial.print(pmw, DEC);
  102. }
  103.  
  104. void resetChars() {
  105.   pin_number = 'x';
  106.   pin_action = 'x';
  107. }
  108.  
  109. void goCommand() {
  110.   if (pin_action != 'M' && pin_action != 'W') { resetChars(); return; }
  111.   int p = findPinFromChar();
  112.   if (p >= MAX_PINS) { resetChars(); return; }
  113.   if (pin_action == 'M') {
  114.     switchMode(pins[p].number);
  115.   }
  116.   if (pin_action == 'W') {
  117.     switchWrite(pins[p].number);
  118.   }
  119.   feedBack();
  120.   resetChars();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement