Guest User

Untitled

a guest
Apr 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. /* Shift Register Test with ShiftPWM */
  2.  
  3. #include <SPI.h>                     //SPI used for faster hardware-based serial communication
  4.  
  5. //-------------SET PINS---------------------------
  6. //Data pin is MOSI (UNO: pin 11, MEGA: pin 51)
  7. const int ShiftPWM_latchPin = 8;          //Latch
  8. //Clock pin is SCK(UNO: pin 13, MEGA: pin 52)
  9. const int ShiftRegisterClear = 7;      //Clear
  10. const bool ShiftPWM_invertOutputs = 0;  //if 1, outputs active low
  11.  
  12. #include <ShiftPWM.h>                //checks for ShiftPWM prefixed values
  13.  
  14. //-------------SET PARAMETERS---------------------------
  15. unsigned char maxBrightness = 255;
  16. unsigned char pwmFrequency = 75;    //In what units?
  17. int numRegisters = 6;        //set this
  18. int numLEDs = 8 * numRegisters;      
  19. int maxRSSI = 40;           //set this
  20. int previousTopLED = 0;      //where Top LED is the topmost illuminated LED
  21.  
  22.  
  23. void setup(){
  24.   pinMode(ShiftPWM_latchPin, OUTPUT);
  25.   SPI.setBitOrder(MSBFIRST);
  26.   SPI.setClockDivider(SPI_CLOCK_DIV4);    //What does this do?
  27.  
  28.   pinMode(ShiftRegisterClear, OUTPUT);  
  29.   SPI.begin();                             //What does this do?
  30.   Serial.begin(9600);
  31.   Serial1.begin(9600);
  32.   Serial.println("Wi-Fi shield version 1");
  33.  
  34.   ShiftPWM.SetAmountOfRegisters(numRegisters);
  35.   ShiftPWM.Start(pwmFrequency, maxBrightness);  
  36. }  
  37.  
  38. //data buffer
  39. char data[128];
  40. int RSSI;
  41.  
  42. void loop(){
  43.   //Print information about the interrupt frequency, duration and load on your program
  44.   //ShiftPWM.PrintInterruptLoad();
  45.  
  46.   //testOutput();
  47.  
  48.   digitalWrite(ShiftRegisterClear, HIGH);
  49.   RSSI = retrieveRSSI(data,64);
  50.   Serial.print("MAX RSSI value is ---> ");
  51.   Serial.println(RSSI);
  52.  
  53.  
  54. //  if(RSSI == 4)
  55. //    Serial.println("The RSSI is not 4! Stop lying you stupid sMMMMhit!!");
  56. //  else{
  57.     RSSIToLEDOutput(RSSI);
  58. //  }
  59. delay(2000);
  60.  
  61. }
  62.  
  63. /*RSSI to LED Output*/
  64. void RSSIToLEDOutput(int RSSI){
  65.   if (RSSI >= 0 && RSSI <=maxRSSI){
  66.  
  67.     //normalize RSSI to a scale appropriate to numLEDs
  68.     int topLED = numLEDs * RSSI / maxRSSI;    //this truncates topLED.
  69.     int topLEDBrightness = modf(maxBrightness * (double)RSSI / (double)maxRSSI, NULL) ;
  70.  
  71.     Serial.print("Output topmost LED: ");
  72.  
  73.     //if RSSI increases, illuminate LEDs
  74.     if (topLED > previousTopLED)
  75.       for (int i = previousTopLED; i< topLED; i++){  
  76.         for (int j = 0; j < maxBrightness; j++){
  77.           ShiftPWM.SetOne(i, j);
  78.           j = 5 + j;
  79.           delay(3);
  80.           //if (interrupt != 0)
  81.             //break;
  82.         }
  83. //        if (interrupt != 0)
  84. //          break;
  85.       }
  86.  
  87.     //if RSSI decreases, turn off LEDs
  88.     if (topLED < previousTopLED)
  89.       for (int i = previousTopLED-1; i>= topLED; i--){
  90.         for (int j = maxBrightness; j > 0; j--){  
  91.           ShiftPWM.SetOne(i, j);
  92.           j = j - 5;
  93.           delay(3);
  94. //          if (interrupt != 0)
  95. //            break;
  96.         }
  97. //        if (interrupt != 0)
  98. //          break;
  99.       }
  100.  
  101.     ShiftPWM.SetOne(topLED, topLEDBrightness);
  102.  
  103.  
  104.     previousTopLED = topLED;          //set up for next round
  105.  
  106.     Serial.println(topLED);
  107.   }
  108.   else {
  109.     Serial.println("Output failed.");
  110.   }
  111. }
  112.  
  113.  
  114. /*Test LED Output*/
  115. void testLEDOutput(){
  116.   ShiftPWM.Set8Pins(0,0b01100011);
  117.   delay(4000);
  118.  
  119.   // Fade in and fade out all outputs one by one fast
  120.   ShiftPWM.OneByOneFast();
  121.  
  122.   // Fade in all outputs
  123.   for(int j=0;j<maxBrightness;j++){
  124.     ShiftPWM.SetAll(j);  
  125.     delay(20);
  126.   }
  127.   // Fade out all outputs
  128.   for(int j=maxBrightness;j>=0;j--){
  129.     ShiftPWM.SetAll(j);  
  130.     delay(20);
  131.   }
  132.  
  133.   // Fade in and fade out all outputs slowly
  134.   ShiftPWM.OneByOneSlow();
  135. }
  136.  
  137. /*
  138. * Extract rssi from:
  139. * SSID_BSSID_Channel_RSSI_Security
  140. * <Sxxxx_xxxxxxxxxxxx_xx_xx_x>
  141. */
  142.  
  143. int extractRSSI(char* data, int buffer_length){
  144.  
  145.   int last_;
  146.   char RSSI[2];
  147.   int RSSIValue;
  148.  
  149.   for(int k = 0;k < buffer_length; k++){
  150.     if(data[k] == '_'){
  151.       last_ = k;      
  152.     }
  153.   }
  154.  
  155.   //clear buffer
  156.   RSSI[0] = 0;
  157.   RSSI[1] = 0;
  158.   int x, j;
  159.   for(j = last_- 1,x = 1; data[j] != '_' && x >= 0; j--, x-- ){
  160.       RSSI[x] = data[j];
  161.   }
  162.  
  163.   RSSIValue = atoi(RSSI);
  164.   //Serial.print("RSSI value is ---> ");
  165.   //Serial.print(RSSIValue);
  166.   //Serial.println(" things");
  167.  
  168.   return RSSIValue;
  169. }
  170.  
  171. void clearDataBuffer(char* data, int buffer_length){
  172.     //clear data buffer
  173.     for(int c = 0; c < buffer_length; c++){
  174.       data[c] = 0;
  175.     }
  176. }
  177.  
  178. /*
  179. * sends command out on UART1 & reads response
  180. *
  181. */
  182. int retrieveRSSI(char* data, int buffer_length){
  183.  
  184.   char inByte;
  185.   int readBytes = 0;
  186.   int i;
  187.   int maxRSSI = 0;
  188.   boolean commandStatus;
  189.   String command = "<DI>";
  190.  
  191.    
  192.     //Serial.println("no bytes in rx buffer sending command "+command+" to shield");
  193.     Serial1.print(command);
  194.     while(Serial1.available()==0) {
  195.  
  196.     }  
  197.     //set data buffer pointer to beginning of data buffer
  198.     i=0;
  199.    
  200.     //Serial.println("reading from shield");
  201.     while(1){
  202.      
  203.       inByte = Serial1.read();
  204.       Serial.print(inByte);
  205.       readBytes++;
  206.  
  207.       if(inByte == '<'){
  208.         // next byte is status of command
  209.         commandStatus = true;
  210.         while (Serial1.available()==0) {
  211.           //do nothing
  212.           delayMicroseconds(30);
  213.         }
  214.         continue;
  215.       }
  216.       if (commandStatus == true) {
  217.         //inByte = Serial1.read();
  218.         //Serial.print(inByte);
  219.         if(inByte == 'S'){
  220.           Serial.println(command+" command was successfull !\nOutput:\n");
  221.           continue;
  222.         }else if(inByte == 'F'){
  223.           Serial.println("command failed :( pmop pmon ...");
  224.           continue;
  225.         }
  226.         commandStatus = false;
  227.       }
  228.      
  229.       if(inByte == ':'){
  230.         Serial.print('\n');
  231.         int t = extractRSSI(data, buffer_length);
  232.         if(t > maxRSSI){
  233.           maxRSSI = t;
  234.         }
  235.         clearDataBuffer(data, buffer_length);
  236.         i=0; // reset buffer pointer
  237.       }else{
  238.         if(i<buffer_length){
  239.           data[i] = inByte;
  240.           i++;
  241.         }
  242.       }
  243.      
  244.       if(inByte == '>'){
  245.         Serial.println("\n end of output");
  246.         break;
  247.       }
  248.       while (Serial1.available()==0) {
  249.       //do nothing
  250.       delayMicroseconds(30);
  251.       }
  252.     }
  253.    
  254.      if(readBytes > 0){
  255.       Serial.print("\nfinished reading : ");
  256.       Serial.print(readBytes);
  257.       Serial.println(" Bytes\n");
  258.       readBytes = 0;
  259.     }  
  260.  
  261.   return maxRSSI;
  262. }
  263.  
  264. void testOutput(){
  265.   // Print information about the interrupt frequency, duration and load on your program
  266.   ShiftPWM.SetAll(0);
  267.   ShiftPWM.PrintInterruptLoad();
  268.  
  269.   // Fade in and fade out all outputs one by one fast. Usefull for testing your circuit
  270.   ShiftPWM.OneByOneFast();
  271.  
  272.   // Fade in all outputs
  273.   for(int j=0;j<maxBrightness;j++){
  274.     ShiftPWM.SetAll(j);  
  275.     delay(20);
  276.   }
  277.   // Fade out all outputs
  278.   for(int j=maxBrightness;j>=0;j--){
  279.     ShiftPWM.SetAll(j);  
  280.     delay(20);
  281.   }
  282.  
  283. }
Add Comment
Please, Sign In to add comment