Guest User

Untitled

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