safwan092

Untitled

Dec 1st, 2018
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define NO_SAMPLES 100u
  2. #define THRESHOLD 5u
  3. // Color Thresholds
  4. #define IDLE_RED_THRESHOLD 287u
  5. #define IDLE_GREEN_THRESHOLD 95u
  6. #define IDLE_BLUE_THRESHOLD 303u
  7. #define RED_RED_THRESHOLD 164u
  8. #define RED_GREEN_THRESHOLD 72u
  9. #define RED_BLUE_THRESHOLD 174u
  10. #define GREEN_RED_THRESHOLD 207u
  11. #define GREEN_GREEN_THRESHOLD 65u
  12. #define GREEN_BLUE_THRESHOLD 219u
  13. #define BLUE_RED_THRESHOLD 227u
  14. #define BLUE_GREEN_THRESHOLD 65u
  15. #define BLUE_BLUE_THRESHOLD 241u
  16. #define L_GRN_RED_THRESHOLD 157u
  17. #define L_GRN_GREEN_THRESHOLD 51u
  18. #define L_GRN_BLUE_THRESHOLD 166u
  19. #define YLOW_RED_THRESHOLD 107u
  20. #define YLOW_GREEN_THRESHOLD 46u
  21. #define YLOW_BLUE_THRESHOLD 114u
  22. #define ORNG_RED_THRESHOLD 121u
  23. #define ORNG_GREEN_THRESHOLD 52u
  24. #define ORNG_BLUE_THRESHOLD 128u
  25. // Sensor Connections
  26. const byte S0 = 8;
  27. const byte S1 = 9;
  28. const byte S2 = 10;
  29. const byte S3 = 11;
  30. const byte Sout = 12;
  31. const byte RED = 3;
  32. const byte GREEN = 5;
  33. const byte BLUE = 6;
  34. uint16_t sumOfSamples = 0;
  35. uint16_t redData = 0;
  36. uint16_t greenData = 0;
  37. uint16_t blueData = 0;
  38. void setup()
  39. {
  40. pinMode(S0, OUTPUT);
  41. pinMode(S1, OUTPUT);
  42. pinMode(S2, OUTPUT);
  43. pinMode(S3, OUTPUT);
  44. pinMode(Sout, INPUT);
  45. // Setting frequency-scaling to 20%
  46. digitalWrite(S0, HIGH);
  47. digitalWrite(S1, LOW);
  48. Serial.begin(115200);
  49. ////////////////////////////////////////////////RGB LED Test
  50. Set_RGB_Color(255u, 255u, 255u);
  51. delay(2000);
  52. Set_RGB_Color(0, 255u, 255u); // Red
  53. delay(2000);
  54. Set_RGB_Color(255u, 0, 255u); // Green
  55. delay(2000);
  56. Set_RGB_Color(255u, 255u, 0); // Blue
  57. delay(2000);
  58. Set_RGB_Color(5, 180, 255); // Yellow
  59. delay(2000);
  60. Set_RGB_Color(5, 55, 205); // Light Green
  61. delay(2000);
  62. Set_RGB_Color(5, 215, 255); // Dark Yellow/ Orange
  63. delay(2000);
  64. ////////////////////////////////////////////////////////////
  65. }
  66. void loop()
  67. {
  68. int i = 0;
  69. // Apply Red Filter
  70. digitalWrite(S2, LOW);
  71. digitalWrite(S3, LOW);
  72. delay(1);
  73. sumOfSamples = 0;
  74. for (i = 0; i < NO_SAMPLES; i++)
  75. {
  76. // Reading the Pulse Width
  77. sumOfSamples += pulseIn(Sout, LOW);
  78. }
  79. redData = sumOfSamples / NO_SAMPLES;
  80. Serial.print("R = ");
  81. Serial.print(redData);
  82. delay(100);
  83. // Apply Green Filter
  84. digitalWrite(S2, HIGH);
  85. digitalWrite(S3, HIGH);
  86. delay(1);
  87. sumOfSamples = 0;
  88. for (i = 0; i < NO_SAMPLES; i++)
  89. {
  90. // Reading the Pulse Width
  91. sumOfSamples += pulseIn(Sout, LOW);
  92. }
  93. greenData = sumOfSamples / NO_SAMPLES;
  94. Serial.print(" G = ");
  95. Serial.print(greenData);
  96. delay(100);
  97. // Apply Blue Filter
  98. digitalWrite(S2, LOW);
  99. digitalWrite(S3, HIGH);
  100. delay(1);
  101. sumOfSamples = 0;
  102. for (i = 0; i < NO_SAMPLES; i++)
  103. {
  104. // Reading the Pulse Width
  105. sumOfSamples += pulseIn(Sout, LOW);
  106. }
  107. blueData = sumOfSamples / NO_SAMPLES;
  108. Serial.print(" B = ");
  109. Serial.print(blueData);
  110. Serial.println(" ");
  111. delay(100);
  112. if ( isIdle() )
  113. {
  114. // Turn-Off All Led's
  115. Serial.println("All Led's Off");
  116. Set_RGB_Color(255u, 255u, 255u);
  117. }
  118. if ( isRed() )
  119. {
  120. // Turn-On Red Led
  121. Serial.println("Red Led");
  122. Set_RGB_Color(0, 255u, 255u); // Red
  123. }
  124. if ( isGreen() )
  125. {
  126. // Turn-On Green Led
  127. Serial.println("Green Led");
  128. Set_RGB_Color(255u, 0, 255u); // Green
  129. }
  130. if ( isBlue() )
  131. {
  132. // Turn-On Blue Led
  133. Serial.println("Blue Led");
  134. Set_RGB_Color(255u, 255u, 0); // Blue
  135. }
  136. if ( isYellow() )
  137. {
  138. // Turn-On Yellow Led
  139. Serial.println("Yellow Led");
  140. Set_RGB_Color(5, 180, 255); // Yellow
  141. }
  142. if ( isLightGreen() )
  143. {
  144. // Turn-On Light Green Led
  145. Serial.println("Light Green Led");
  146. Set_RGB_Color(5, 55, 205); // Light Green
  147. }
  148. if ( isOrange() )
  149. {
  150. // Turn-On Orange Led
  151. Serial.println("Orange Led");
  152. Set_RGB_Color(5, 215, 255); // Dark Yellow/ Orange
  153. }
  154. delay(2000);
  155. }
  156. boolean isIdle( void )
  157. {
  158. boolean status = false;
  159. if ( ((IDLE_RED_THRESHOLD - THRESHOLD) < redData)
  160. && (redData < (IDLE_RED_THRESHOLD + THRESHOLD)) )
  161. {
  162. if ( ((IDLE_GREEN_THRESHOLD - THRESHOLD) < greenData)
  163. && (greenData < (IDLE_GREEN_THRESHOLD + THRESHOLD)) )
  164. {
  165. if ( ((IDLE_BLUE_THRESHOLD - THRESHOLD) < blueData)
  166. && (blueData < (IDLE_BLUE_THRESHOLD + THRESHOLD)) )
  167. {
  168. status = true;
  169. }
  170. }
  171. }
  172. return status;
  173. }
  174. boolean isRed( void )
  175. {
  176. boolean status = false;
  177. if ( ((RED_RED_THRESHOLD - THRESHOLD) < redData)
  178. && (redData < (RED_RED_THRESHOLD + THRESHOLD)) )
  179. {
  180. if ( ((RED_GREEN_THRESHOLD - THRESHOLD) < greenData)
  181. && (greenData < (RED_GREEN_THRESHOLD + THRESHOLD)) )
  182. {
  183. if ( ((RED_BLUE_THRESHOLD - THRESHOLD) < blueData)
  184. && (blueData < (RED_BLUE_THRESHOLD + THRESHOLD)) )
  185. {
  186. status = true;
  187. }
  188. }
  189. }
  190. return status;
  191. }
  192. boolean isGreen( void )
  193. {
  194. boolean status = false;
  195. if ( ((GREEN_RED_THRESHOLD - THRESHOLD) < redData)
  196. && (redData < (GREEN_RED_THRESHOLD + THRESHOLD)) )
  197. {
  198. if ( ((GREEN_GREEN_THRESHOLD - THRESHOLD) < greenData)
  199. && (greenData < (GREEN_GREEN_THRESHOLD + THRESHOLD)) )
  200. {
  201. if ( ((GREEN_BLUE_THRESHOLD - THRESHOLD) < blueData)
  202. && (blueData < (GREEN_BLUE_THRESHOLD + THRESHOLD)) )
  203. {
  204. status = true;
  205. }
  206. }
  207. }
  208. return status;
  209. }
  210. boolean isBlue( void )
  211. {
  212. boolean status = false;
  213. if ( ((BLUE_RED_THRESHOLD - THRESHOLD) < redData)
  214. && (redData < (BLUE_RED_THRESHOLD + THRESHOLD)) )
  215. {
  216. if ( ((BLUE_GREEN_THRESHOLD - THRESHOLD) < greenData)
  217. && (greenData < (BLUE_GREEN_THRESHOLD + THRESHOLD)) )
  218. {
  219. if ( ((BLUE_BLUE_THRESHOLD - THRESHOLD) < blueData)
  220. && (blueData < (BLUE_BLUE_THRESHOLD + THRESHOLD)) )
  221. {
  222. status = true;
  223. }
  224. }
  225. }
  226. return status;
  227. }
  228. boolean isLightGreen( void )
  229. {
  230. boolean status = false;
  231. if ( ((L_GRN_RED_THRESHOLD - THRESHOLD) < redData)
  232. && (redData < (L_GRN_RED_THRESHOLD + THRESHOLD)) )
  233. {
  234. if ( ((L_GRN_GREEN_THRESHOLD - THRESHOLD) < greenData)
  235. && (greenData < (L_GRN_GREEN_THRESHOLD + THRESHOLD)) )
  236. {
  237. if ( ((L_GRN_BLUE_THRESHOLD - THRESHOLD) < blueData)
  238. && (blueData < (L_GRN_BLUE_THRESHOLD + THRESHOLD)) )
  239. {
  240. status = true;
  241. }
  242. }
  243. }
  244. return status;
  245. }
  246. boolean isYellow( void )
  247. {
  248. boolean status = false;
  249. if ( ((YLOW_RED_THRESHOLD - THRESHOLD) < redData)
  250. && (redData < (YLOW_RED_THRESHOLD + THRESHOLD)) )
  251. {
  252. if ( ((YLOW_GREEN_THRESHOLD - THRESHOLD) < greenData)
  253. && (greenData < (YLOW_GREEN_THRESHOLD + THRESHOLD)) )
  254. {
  255. if ( ((YLOW_BLUE_THRESHOLD - THRESHOLD) < blueData)
  256. && (blueData < (YLOW_BLUE_THRESHOLD + THRESHOLD)) )
  257. {
  258. status = true;
  259. }
  260. }
  261. }
  262. return status;
  263. }
  264. boolean isOrange( void )
  265. {
  266. boolean status = false;
  267. if ( ((ORNG_RED_THRESHOLD - THRESHOLD) < redData)
  268. && (redData < (ORNG_RED_THRESHOLD + THRESHOLD)) )
  269. {
  270. if ( ((ORNG_GREEN_THRESHOLD - THRESHOLD) < greenData)
  271. && (greenData < (ORNG_GREEN_THRESHOLD + THRESHOLD)) )
  272. {
  273. if ( ((ORNG_BLUE_THRESHOLD - THRESHOLD) < blueData)
  274. && (blueData < (ORNG_BLUE_THRESHOLD + THRESHOLD)) )
  275. {
  276. status = true;
  277. }
  278. }
  279. }
  280. return status;
  281. }
  282. void Set_RGB_Color( uint8_t red, uint8_t green, uint8_t blue)
  283. {
  284. analogWrite(RED, red);
  285. analogWrite(GREEN, green);
  286. analogWrite(BLUE, blue);
  287. }
RAW Paste Data