Guest User

Arduino SOCD modified with LED

a guest
Aug 24th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.75 KB | None | 0 0
  1. #include <EEPROM.h>
  2.  
  3. const uint8_t eepromAddress = 0;
  4.  
  5. const uint8_t upOUT = 2; // to button/switch
  6. const uint8_t downOUT = 3; // to button/switch
  7. const uint8_t leftOUT = 4; // to button/switch
  8. const uint8_t rightOUT = 5; // to button/switch
  9.  
  10. const uint8_t upIN = 6; // from pcb
  11. const uint8_t downIN = 7; // from pcb
  12. const uint8_t leftIN = 8; // from pcb
  13. const uint8_t rightIN = 9; // from pcb
  14.  
  15. const uint8_t socdLedRED = 10;
  16. const uint8_t socdLedGREEN = 16;
  17. const uint8_t socdLedBLUE = 14;
  18. const uint8_t socdFourLED = 15;
  19.  
  20. uint8_t leftRead;
  21. uint8_t downRead;
  22. uint8_t upRead;
  23. uint8_t rightRead;
  24.  
  25. unsigned long currentMillis;
  26. unsigned long upPreviousMillis = 0;
  27. unsigned long downPreviousMillis = 0;
  28. unsigned long leftPreviousMillis = 0;
  29. unsigned long rightPreviousMillis = 0;
  30.  
  31. uint8_t upPreviousState = 0;
  32. uint8_t downPreviousState = 0;
  33. uint8_t leftPreviousState = 0;
  34. uint8_t rightPreviousState = 0;
  35.  
  36. uint8_t mode;
  37.  
  38. void setup() {
  39.   // Serial.begin(9600);
  40.  
  41.   pinMode(leftOUT, INPUT_PULLUP);
  42.   pinMode(rightOUT, INPUT_PULLUP);
  43.   pinMode(upOUT, INPUT_PULLUP);
  44.   pinMode(downOUT, INPUT_PULLUP);
  45.  
  46.   pinMode(leftIN, INPUT);
  47.   pinMode(rightIN, INPUT);
  48.   pinMode(downIN, INPUT);
  49.   pinMode(upIN, INPUT);
  50.  
  51.   pinMode(socdLedRED, OUTPUT);
  52.   pinMode(socdLedGREEN, OUTPUT);
  53.   pinMode(socdLedBLUE, OUTPUT);
  54.  
  55.   leftRead = digitalRead(leftOUT);
  56.   rightRead = digitalRead(rightOUT);
  57.   upRead = digitalRead(upOUT);
  58.   downRead = digitalRead(downOUT);
  59.  
  60.   // so !variable means button pressed
  61.   // if (leftRead && rightRead)  mode = 3; // default
  62.   // if (!leftRead && rightRead) mode = 1; // left held down
  63.   // if (leftRead && !rightRead) mode = 2; // right held down
  64.   // if (!leftRead && !rightRead) mode = 0; // left & right held down
  65.  
  66.   if (leftRead && rightRead && upRead && downRead) mode = EEPROM.read(eepromAddress); // default - read from eeprom
  67.   if (!leftRead && rightRead && upRead && downRead) // left held down
  68.   {
  69.     mode = 0;
  70.     EEPROM.write(eepromAddress, mode);
  71.   }
  72.   if (!rightRead && leftRead && upRead && downRead) // right held down
  73.   {
  74.     mode = 1;
  75.     EEPROM.write(eepromAddress, mode);
  76.   }
  77.   if (!upRead && rightRead && leftRead && downRead) // up held down
  78.   {
  79.     mode = 2;
  80.     EEPROM.write(eepromAddress, mode);
  81.   }
  82.   if (!downRead && rightRead && upRead && leftRead) // down held down
  83.   {
  84.     mode = 3;
  85.     EEPROM.write(eepromAddress, mode);
  86.   }
  87.  
  88.   if (mode == 0)
  89.   {
  90.     digitalWrite(socdLedRED, LOW);
  91.     digitalWrite(socdLedGREEN, HIGH);
  92.     digitalWrite(socdLedBLUE, HIGH);
  93.   }
  94.   if (mode == 1)
  95.   {
  96.     digitalWrite(socdLedRED, LOW);
  97.     digitalWrite(socdLedGREEN, HIGH);
  98.     digitalWrite(socdLedBLUE, LOW);
  99.   }
  100.   if (mode == 2)
  101.   {
  102.     digitalWrite(socdLedRED, HIGH);
  103.     digitalWrite(socdLedGREEN, LOW);
  104.     digitalWrite(socdLedBLUE, HIGH);
  105.   }
  106.   if (mode == 3)
  107.   {
  108.     digitalWrite(socdLedRED, HIGH);
  109.     digitalWrite(socdLedGREEN, HIGH);
  110.     digitalWrite(socdLedBLUE, LOW);
  111.   }
  112. }
  113.  
  114. void loop() {
  115.   currentMillis = millis();
  116.  
  117.   // Serial.println(mode);
  118.  
  119.   // 0 is down, 1 is up (0 = true)
  120.   upRead = digitalRead(upOUT);
  121.   downRead = digitalRead(downOUT);
  122.   leftRead = digitalRead(leftOUT);
  123.   rightRead = digitalRead(rightOUT);
  124.  
  125.   switch (mode)
  126.   {
  127.     case 0:
  128.       outputSOCD0(); // outputRAW
  129.       break;
  130.  
  131.     case 1:
  132.       outputSOCD1(); // outputSODC
  133.       break;
  134.  
  135.     case 2:
  136.       outputSOCD2(); // outputLASTCOMMAND
  137.       break;
  138.  
  139.     case 3:
  140.       outputSOCD3(); // outputLASTCOMMANDnSODC
  141.       break;
  142.  
  143.     default:
  144.       mode = 0;
  145.       break;
  146.   }
  147. }
  148.  
  149. // raw output, but for dualsense: left + right = neutral; up + down = neutral
  150. void outputSOCD0() { // outputRAW
  151.   if (!upRead)    pinMode(upIN, OUTPUT);
  152.   digitalWrite(upIN, LOW);
  153.   if (upRead)     pinMode(upIN, INPUT);
  154.  
  155.   if (!downRead)  pinMode(downIN, OUTPUT);
  156.   digitalWrite(downIN, LOW);
  157.   if (downRead)   pinMode(downIN, INPUT);
  158.  
  159.   if (!leftRead)  pinMode(leftIN, OUTPUT);
  160.   digitalWrite(leftIN, LOW);
  161.   if (leftRead)   pinMode(leftIN, INPUT);
  162.  
  163.   if (!rightRead) pinMode(rightIN, OUTPUT);
  164.   digitalWrite(rightIN, LOW);
  165.   if (rightRead)  pinMode(rightIN, INPUT);
  166. }
  167.  
  168. // left + right = neutral; down + up = up
  169. void outputSOCD1() { // outputSODC
  170.   if (!leftRead)
  171.   {
  172.     if (rightRead)
  173.     {
  174.       pinMode(rightIN, INPUT);
  175.       pinMode(leftIN, OUTPUT);
  176.       digitalWrite(leftIN, LOW);
  177.     }
  178.     else
  179.     {
  180.       pinMode(rightIN, INPUT);
  181.       pinMode(leftIN, INPUT);
  182.     }
  183.   }
  184.   else if (!rightRead)
  185.   {
  186.     pinMode(leftIN, INPUT);
  187.     pinMode(rightIN, OUTPUT);
  188.     digitalWrite(rightIN, LOW);
  189.   }
  190.   else
  191.   {
  192.     pinMode(rightIN, INPUT);
  193.     pinMode(leftIN, INPUT);
  194.   }
  195.  
  196.  
  197.   if (!upRead)
  198.   {
  199.     pinMode(downIN, INPUT);
  200.     pinMode(upIN, OUTPUT);
  201.     digitalWrite(upIN, LOW);
  202.   }
  203.   else if (!downRead)
  204.   {
  205.     {
  206.       pinMode(downIN, OUTPUT);
  207.       digitalWrite(downIN, LOW);
  208.       pinMode(upIN, INPUT);
  209.     }
  210.   }
  211.   else
  212.   {
  213.     pinMode(upIN, INPUT);
  214.     pinMode(downIN, INPUT);
  215.   }
  216.  
  217. }
  218.  
  219. // left + right = last command wins; up + down = last command wins
  220. void outputSOCD2() { // outputLASTCOMMAND
  221.   if (!upRead) {
  222.     if (!upPreviousState) {
  223.       upPreviousMillis = currentMillis;
  224.       upPreviousState = HIGH;
  225.     }
  226.  
  227.     if (!downRead) {
  228.       if (upPreviousMillis > downPreviousMillis) {
  229.         pinMode(upIN, OUTPUT);
  230.         digitalWrite(upIN, LOW);
  231.       }
  232.       else
  233.       {
  234.         pinMode(upIN, INPUT);
  235.       }
  236.  
  237.     }
  238.     else
  239.     {
  240.       pinMode(upIN, OUTPUT);
  241.       digitalWrite(upIN, LOW);
  242.     }
  243.   }
  244.  
  245.  
  246.   if (upRead) {
  247.     upPreviousState = LOW;
  248.     pinMode(upIN, INPUT);
  249.  
  250.   }
  251.  
  252.   if (!downRead) {
  253.     if (!downPreviousState) {
  254.       downPreviousMillis = currentMillis;
  255.       downPreviousState = HIGH;
  256.     }
  257.  
  258.     if (!upRead) {
  259.       if (downPreviousMillis > upPreviousMillis) {
  260.         pinMode(downIN, OUTPUT);
  261.         digitalWrite(downIN, LOW);
  262.       }
  263.       else
  264.       {
  265.         pinMode(downIN, INPUT);
  266.       }
  267.  
  268.     }
  269.     else
  270.     {
  271.       pinMode(downIN, OUTPUT);
  272.       digitalWrite(downIN, LOW);
  273.     }
  274.   }
  275.  
  276.  
  277.   if (downRead) {
  278.     downPreviousState = LOW;
  279.     pinMode(downIN, INPUT);
  280.  
  281.   }
  282.  
  283.   if (!leftRead) {
  284.     if (!leftPreviousState) {
  285.       leftPreviousMillis = currentMillis;
  286.       leftPreviousState = HIGH;
  287.     }
  288.  
  289.     if (!rightRead) {
  290.       if (leftPreviousMillis > rightPreviousMillis) {
  291.        
  292.         pinMode(leftIN, OUTPUT);
  293.         digitalWrite(leftIN, LOW);
  294.       }
  295.       else
  296.       {
  297.         pinMode(leftIN, INPUT);
  298.       }
  299.  
  300.     }
  301.     else
  302.     {
  303.      pinMode(leftIN, OUTPUT);
  304.         digitalWrite(leftIN, LOW);
  305.       }
  306.   }
  307.  
  308.  
  309.   if (leftRead) {
  310.     leftPreviousState = LOW;
  311.     pinMode(leftIN, INPUT);
  312.    
  313.  
  314.   }
  315.   if (!rightRead) {
  316.     if (!rightPreviousState) {
  317.       rightPreviousMillis = currentMillis;
  318.       rightPreviousState = HIGH;
  319.     }
  320.  
  321.     if (!leftRead) {
  322.       if (rightPreviousMillis > leftPreviousMillis) {
  323.        pinMode(rightIN, OUTPUT);
  324.         digitalWrite(rightIN, LOW);
  325.       }
  326.       else
  327.       {
  328.         pinMode(rightIN, INPUT);
  329.       }
  330.  
  331.     }
  332.     else
  333.     {
  334.       pinMode(rightIN, OUTPUT);
  335.       digitalWrite(rightIN, LOW);
  336.     }
  337.   }
  338.  
  339.   if (rightRead) {
  340.     rightPreviousState = LOW;
  341.     pinMode(rightIN, INPUT);
  342.  
  343.   }
  344. }
  345.  
  346. // up + down = up ; left + right = last command wins
  347. void outputSOCD3() { // outputLASTCOMMANDnSODC
  348.   if (!upRead)
  349.   {
  350.     pinMode(downIN, INPUT);
  351.     pinMode(upIN, OUTPUT);
  352.     digitalWrite(upIN, LOW);
  353.   }
  354.   else if (!downRead)
  355.   {
  356.     {
  357.       pinMode(downIN, OUTPUT);
  358.       digitalWrite(downIN, LOW);
  359.       pinMode(upIN, INPUT);
  360.     }
  361.   }
  362.   else
  363.   {
  364.     pinMode(upIN, INPUT);
  365.     pinMode(downIN, INPUT);
  366.   }
  367.  
  368.  
  369.  if (!leftRead) {
  370.     if (!leftPreviousState) {
  371.       leftPreviousMillis = currentMillis;
  372.       leftPreviousState = HIGH;
  373.     }
  374.     if (!rightRead) {
  375.       if (leftPreviousMillis > rightPreviousMillis) {
  376.        
  377.         pinMode(leftIN, OUTPUT);
  378.         digitalWrite(leftIN, LOW);
  379.       }
  380.       else
  381.       {
  382.         pinMode(leftIN, INPUT);
  383.       }
  384.  
  385.     }
  386.     else
  387.     {
  388.      pinMode(leftIN, OUTPUT);
  389.         digitalWrite(leftIN, LOW);
  390.       }
  391.   }
  392.  
  393.  
  394.   if (leftRead) {
  395.     leftPreviousState = LOW;
  396.     pinMode(leftIN, INPUT);
  397.    
  398.  
  399.   }
  400.  
  401.   if (!rightRead) {
  402.     if (!rightPreviousState) {
  403.       rightPreviousMillis = currentMillis;
  404.       rightPreviousState = HIGH;
  405.     }
  406.     if (!leftRead) {
  407.       if (rightPreviousMillis > leftPreviousMillis) {
  408.        pinMode(rightIN, OUTPUT);
  409.         digitalWrite(rightIN, LOW);
  410.       }
  411.       else
  412.       {
  413.         pinMode(rightIN, INPUT);
  414.       }
  415.  
  416.     }
  417.     else
  418.     {
  419.       pinMode(rightIN, OUTPUT);
  420.       digitalWrite(rightIN, LOW);
  421.     }
  422.   }
  423.  
  424.   if (rightRead) {
  425.     rightPreviousState = LOW;
  426.     pinMode(rightIN, INPUT);
  427.  
  428.   }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment