Advertisement
Guest User

led_and_pot_serial.ino

a guest
Nov 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //    Copyright 2013 Christopher D. McMurrough
  3. //
  4. //    This program is free software: you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation, either version 3 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. //
  17.  
  18. /***********************************************************************************************************************
  19. * @FILE serial_communication_variable.ino
  20. * @BRIEF An example Arduino sketch showing USB-serial communications with the Teensy microcontroller
  21. *
  22. * This program provides an example of USB-serial communications with the Teensy 3.1/3.2 microcontroller. The communication
  23. * is based on variable width byte packets containing an error checksum. The packet structure is defined as follows:
  24. *
  25. * packet[0] = PACKET_START_BYTE (0xAA)
  26. * packet[1] = PACKET_SIZE (total number of bytes including overhead and payload)
  27. * packet[n+2] = payload byte n -> [0, PAYLOAD_SIZE - 1]
  28. * packet[PACKET_SIZE - 1] = packet checksum
  29. *
  30. * The checksum is computed as the XOR chain of each byte in the packet before the checksum:
  31. * packet[0] XOR packet[1] XOR ... XOR packet[PACKET_SIZE - 2]
  32. *
  33. * @AUTHOR Christopher D. McMurrough
  34. **********************************************************************************************************************/
  35.  
  36. // define GPIO pins
  37. const int LED_PIN = 13;
  38. const int LED_ON = HIGH;
  39. const int LED_OFF = LOW;
  40.  
  41. const int RED_PIN = 21;
  42. const int GREEN_PIN = 22;
  43. const int BLUE_PIN = 23;
  44. const int RGB_ON = 0;
  45. const int RGB_OFF = 255;
  46.  
  47. // define potentiometer pins
  48. const int POT_PIN = 14;
  49.  
  50. // define serial communication parameters
  51. const unsigned long BAUD_RATE = 9600;
  52.  
  53. // define packet parameters
  54. const byte PACKET_START_BYTE = 0xAA;
  55. const unsigned int PACKET_OVERHEAD_BYTES = 3;
  56. const unsigned int PACKET_MIN_BYTES = PACKET_OVERHEAD_BYTES + 1;
  57. const unsigned int PACKET_MAX_BYTES = 255;
  58.  
  59. int prev_potValue = 0;
  60.  
  61. /***********************************************************************************************************************
  62. * @BRIEF perform initial setup of the microcontroller
  63. * @AUTHOR Christoper D. McMurrough
  64. **********************************************************************************************************************/
  65. void setup()
  66. {
  67.     // initialize the IO pins
  68.     pinMode(LED_PIN, OUTPUT);
  69.     pinMode(RED_PIN, OUTPUT);
  70.     pinMode(GREEN_PIN, OUTPUT);
  71.     pinMode(BLUE_PIN, OUTPUT);
  72.  
  73.     // turn off the RGB LED by pulling the pins hig
  74.     digitalWrite(RED_PIN, RGB_OFF);
  75.     digitalWrite(GREEN_PIN, RGB_OFF);
  76.     digitalWrite(BLUE_PIN, RGB_OFF);
  77.  
  78.     analogWriteResolution(16);
  79.  
  80.     // initialize the serial port
  81.     Serial.begin(BAUD_RATE);
  82.  
  83.     // flash the LED state
  84.     for(int i = 0; i < 25; i++)
  85.     {
  86.         digitalWrite(LED_PIN, LED_ON);
  87.         delay(50);
  88.         digitalWrite(LED_PIN, LED_OFF);
  89.         delay(50);
  90.     }
  91. }
  92.  
  93. /***********************************************************************************************************************
  94. * @BRIEF assembles and transmits a serial packet containing the given payload
  95. * @PARAM[in] payloadSize the size of the given payload in bytes
  96. * @PARAM[in] payload pointer to the data payload array
  97. * @RETURN true if the packet was transmitted successfully
  98. * @AUTHOR Christoper D. McMurrough
  99. **********************************************************************************************************************/
  100. boolean sendPacket(unsigned int payloadSize, byte *payload)
  101. {
  102.     // check for max payload size
  103.     unsigned int packetSize = payloadSize + PACKET_OVERHEAD_BYTES;
  104.     if(packetSize > PACKET_MAX_BYTES)
  105.     {
  106.         return false;
  107.     }
  108.  
  109.     // create the serial packet transmit buffer
  110.     static byte packet[PACKET_MAX_BYTES];
  111.  
  112.     // populate the overhead fields
  113.     packet[0] = PACKET_START_BYTE;
  114.     packet[1] = packetSize;
  115.     byte checkSum = packet[0] ^ packet[1];
  116.  
  117.     // populate the packet payload while computing the checksum
  118.     for(unsigned int i = 0; i < payloadSize; i++)
  119.     {
  120.         packet[i + 2] = payload[i];
  121.         checkSum = checkSum ^ packet[i + 2];
  122.     }
  123.  
  124.     // store the checksum
  125.     packet[packetSize - 1] = checkSum;
  126.  
  127.     // send the packet
  128.     Serial.write(packet, packetSize);
  129.     Serial.flush();
  130.     return true;
  131. }
  132.  
  133.  
  134. /***********************************************************************************************************************
  135. * @BRIEF checks to see if the given packet is complete and valid
  136. * @PARAM[in] packetSize the size of the given packet buffer in bytes
  137. * @PARAM[in] packet pointer to the packet buffer
  138. * @RETURN true if the packet is valid
  139. * @AUTHOR Christoper D. McMurrough
  140. **********************************************************************************************************************/
  141. boolean validatePacket(unsigned int packetSize, byte *packet)
  142. {
  143.     // check the packet size
  144.     if(packetSize < PACKET_MIN_BYTES || packetSize > PACKET_MAX_BYTES)
  145.     {
  146.         return false;
  147.     }
  148.  
  149.     // check the start byte
  150.     if(packet[0] != PACKET_START_BYTE)
  151.     {
  152.         return false;
  153.     }
  154.  
  155.     // check the length byte
  156.     if(packet[1] != packetSize)
  157.     {
  158.         return false;
  159.     }
  160.  
  161.     // compute the checksum
  162.     byte checksum = 0x00;
  163.     for(unsigned int i = 0; i < packetSize - 1; i++)
  164.     {
  165.         checksum = checksum ^ packet[i];
  166.     }
  167.  
  168.     // check to see if the computed checksum and packet checksum are equal
  169.     if(packet[packetSize - 1] != checksum)
  170.     {
  171.         return false;
  172.     }
  173.  
  174.     // all validation checks passed, the packet is valid
  175.     return true;
  176. }
  177.  
  178. /***********************************************************************************************************************
  179. * @BRIEF main program loop
  180. * @AUTHOR Christoper D. McMurrough
  181. **********************************************************************************************************************/
  182. void loop()
  183. {
  184.     // define control variables
  185.     boolean isRunning = true;
  186.     boolean ledState = false;
  187.  
  188.     // create the serial packet receive buffer
  189.     static byte buffer[PACKET_MAX_BYTES];
  190.     unsigned int count = 0;
  191.     unsigned int packetSize = PACKET_MIN_BYTES;
  192.  
  193.  
  194.     // continuously check for received packets
  195.     while(isRunning)
  196.     {
  197.  
  198.         if(abs(prev_potValue - analogRead(POT_PIN)) >= 30){
  199.           byte load[3];
  200.           load[0] = 'P';
  201.           load[1] = analogRead(POT_PIN)>>8;
  202.           load[2] = analogRead(POT_PIN)&0xFF;
  203.           sendPacket(3, load);
  204.           prev_potValue = analogRead(POT_PIN);
  205.          
  206.  
  207.        
  208.         }
  209.         // check to see if serial byte is available
  210.         if(Serial.available())
  211.         {
  212.             // get the byte
  213.             byte b = Serial.read();
  214.  
  215.             // handle the byte according to the current count
  216.             if(count == 0 && b == PACKET_START_BYTE)
  217.             {
  218.                 // this byte signals the beginning of a new packet
  219.                 buffer[count] = b;
  220.                 count++;
  221.                 continue;
  222.             }
  223.             else if(count == 0)
  224.             {
  225.                 // the first byte is not valid, ignore it and continue
  226.                 continue;
  227.             }
  228.             else if(count == 1)
  229.             {
  230.                 // this byte contains the overall packet length
  231.                 buffer[count] = b;
  232.  
  233.                 // reset the count if the packet length is not in range
  234.                 if(packetSize < PACKET_MIN_BYTES || packetSize > PACKET_MAX_BYTES)
  235.                 {
  236.                     count = 0;
  237.                 }
  238.                 else
  239.                 {
  240.                     packetSize = b;
  241.                     count++;
  242.                 }
  243.                 continue;
  244.             }
  245.             else if(count < packetSize)
  246.             {
  247.                 // store the byte
  248.                 buffer[count] = b;
  249.                 count++;
  250.             }
  251.  
  252.             // check to see if we have acquired enough bytes for a full packet
  253.             if(count >= packetSize)
  254.             {
  255.                 // validate the packet
  256.                 if(validatePacket(packetSize, buffer))
  257.                 {
  258.                     // change the LED state if the packet is valid
  259.                     ledState = !ledState;
  260.                     digitalWrite(LED_PIN, ledState);
  261.  
  262.                     if(buffer[2]=='L')
  263.                     {
  264.                       analogWrite(RED_PIN, 65535 - ((int)buffer[5]<<8));
  265.                       analogWrite(GREEN_PIN, 65535 - ((int)buffer[4]<<8));  
  266.                       analogWrite(BLUE_PIN, 65535 - ((int)buffer[3]<<8));  
  267.                     }
  268.  
  269.  
  270.                    
  271.                    
  272.                    
  273.                 }
  274.  
  275.                 // reset the count
  276.                 count = 0;
  277.             }
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement