Advertisement
tpaper

SUPER-BETA M&M's sorter

Feb 22nd, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #define RBLU 271
  2. #define RVERDE 338
  3. #define RARANCIO 562
  4. #define RROSSO 451
  5. #define RMARRONE 321
  6. #define RGIALLO 615
  7.  
  8. #define GBLU 340
  9. #define GVERDE 455
  10. #define GARANCIO 450
  11. #define GROSSO 337
  12. #define GMARRONE 323
  13. #define GGIALLO 697
  14.  
  15. #define BBLU 280
  16. #define BVERDE 192
  17. #define BARANCIO 171
  18. #define BROSSO 161
  19. #define BMARRONE 156
  20. #define BGIALLO 208
  21.  
  22. #include <Servo.h>
  23.  
  24. Servo myservo; // create servo object to control a servo
  25. Servo canale;
  26. // a maximum of eight servo objects can be created
  27.  
  28. int inz = 0; // variable to store the servo position
  29.  
  30. int trip[3] = {0,0,0};
  31. int pos[4] = {60,90,120,150};
  32.  
  33. void setup()
  34. {
  35. Serial.begin(9600);
  36. myservo.attach(9);
  37. canale.attach(10);
  38. pinMode(2,INPUT);
  39. pinMode(3,INPUT);
  40. pinMode(4,OUTPUT);
  41. pinMode(13,OUTPUT);
  42. analogReference(INTERNAL);
  43.  
  44.  
  45.  
  46. }
  47.  
  48.  
  49. void loop()
  50. {
  51. for(int i=0;i<4;i++) {
  52. delay(2000);
  53. canale.write(pos[i]);
  54. }
  55.  
  56. myservo.write(104);
  57.  
  58. while(!digitalRead(2));
  59.  
  60. inz = millis();
  61. delay(400);
  62. while(!digitalRead(2));
  63. inz = millis()-inz;
  64.  
  65.  
  66. while(true) {
  67. while(digitalRead(2));
  68. myservo.write(97);
  69. digitalWrite(13,HIGH);
  70.  
  71.  
  72. lol();
  73. Serial.print("Posiziono canale:");
  74. Serial.println(trip[1]);
  75. canale.write(pos[trip[1]]);
  76. //while(digitalRead(3));
  77. myservo.write(104);
  78. digitalWrite(13,LOW);
  79. delay(inz-200);
  80. myservo.write(101);
  81. while(!digitalRead(2));
  82. }
  83. }
  84.  
  85.  
  86. void lol() {
  87. int r,g,b;
  88. int l;
  89. int letture[11] = {0,0,0,0,0,0,0,0,0,0,0};
  90. //Serial.println("\n\n\nOra:");
  91. Serial.print("\n");
  92. for(int i=0;i<5;i++) {
  93. digitalWrite(4,HIGH);
  94. delay(8);
  95. r = analogRead(0);
  96. delay(3);
  97. g = analogRead(1);
  98. delay(3);
  99. b = analogRead(2);
  100. digitalWrite(4,LOW);
  101.  
  102. //Serial.print(" (");
  103. Serial.print(r,DEC);
  104. Serial.print("\t");
  105. Serial.print(g,DEC);
  106. Serial.print("\t");
  107. Serial.print(b,DEC);
  108. Serial.print("\n");
  109. delay(10);
  110.  
  111. letture[getscore(r,g,b)] += 1;
  112.  
  113. }
  114. Serial.print("-->");
  115. l = getmaxl(letture);
  116.  
  117. if (l == 3) { l=2; }
  118. if (l == 4) { l=3; }
  119.  
  120.  
  121. Serial.println(getmaxl(letture), DEC);
  122. trip[0] = trip[1];
  123. trip[1] = trip[2];
  124. trip[2] = l;
  125.  
  126.  
  127.  
  128.  
  129. }
  130. // 0 1 2 3 4 5
  131. //char nomi[][] = {"BLU", "VERDE", "ARANCIO", "ROSSO", "MARRONE", "GIALLO"};
  132. int getscore(int r, int g, int b) {
  133. // BLU VERDE ARAN ROSS MARR GIAL
  134. int score[6] = { 1000, 1000, 1000, 1000, 1000, 1000};
  135. score[0] = abs(RBLU - r) + abs(GBLU - g) + abs(BBLU - b);
  136. score[1] = abs(RVERDE - r) + abs(GVERDE - g) + abs(BVERDE - b);
  137. score[2] = abs(RARANCIO - r) + abs(GARANCIO - g) + abs(BARANCIO - b);
  138. score[3] = abs(RROSSO - r) + abs(GROSSO - g) + abs(BROSSO - b);
  139. score[4] = abs(RMARRONE - r) + abs(GMARRONE - g) + abs(BMARRONE - b);
  140. score[5] = abs(RGIALLO - r) + abs(GGIALLO - g) + abs(BGIALLO - b);
  141.  
  142. Serial.print("PUNTEGGI: ");
  143. for (int i=0;i<6;i++) {
  144. Serial.print(score[i],DEC);
  145. Serial.print(",");
  146. }
  147. Serial.print(" --- ");
  148. Serial.print(getmax(score),DEC);
  149. Serial.print("\n");
  150. return getmax(score);
  151.  
  152. //getmax(score);
  153.  
  154. }
  155.  
  156. int getmax(int score[6]) {
  157. int small = score[0];
  158. int indez = 0;
  159. for (int i=1;i<6;i++) {
  160. if (score[i] < small) { small = score[i]; indez = i;}
  161. }
  162.  
  163. return indez;
  164. }
  165.  
  166. int getmaxl(int score[11]) {
  167. int small = score[0];
  168. int indez = 0;
  169. for (int i=1;i<11;i++) {
  170. if (score[i] > small) { small = score[i]; indez = i;}
  171. }
  172.  
  173. return indez;
  174. }
  175.  
  176. boolean compreso(int num,int a, int b, int offset) {
  177. if (a>b) {
  178. a = a + offset;
  179. b = b - offset;
  180. if ((num < a) && (num > b)) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. } else {
  186. return (compreso(num,b,a,offset));
  187. }
  188. }
  189.  
  190. void rec(int r, int g, int b, int offset) {
  191. if (compreso(r,265,240,offset)) {
  192. if (compreso(g,165,150,offset)) {
  193. if (compreso(b,80,70,offset)) {
  194. Serial.println("Faccia rossa");
  195. return;
  196. }
  197. }
  198. }
  199. if (compreso(r,130,120,offset)) {
  200. if (compreso(g,205,90,offset)) {
  201. if (compreso(b,85,75,offset)) {
  202. Serial.println("Faccia verde");
  203. return;
  204. }
  205. }
  206. }
  207. if (compreso(r,334,315,offset)) {
  208. if (compreso(g,310,295,offset)) {
  209. if (compreso(b,80,90,offset)) {
  210. Serial.println("Faccia arancio");
  211. return;
  212. }
  213. }
  214. }
  215. if (compreso(r,130,140,offset)) {
  216. if (compreso(g,173,183,offset)) {
  217. if (compreso(b,163,171,offset)) {
  218. Serial.println("Faccia blu");
  219. return;
  220. }
  221. }
  222. }
  223. if (compreso(r,337,352,offset)) {
  224. if (compreso(g,382,368,offset)) {
  225. if (compreso(b,105,97,offset)) {
  226. Serial.println("Faccia gialla");
  227. return;
  228. }
  229. }
  230. }
  231. if (compreso(r,337,354,offset)) {
  232. if (compreso(g,334,358,offset)) {
  233. if (compreso(b,227,217,offset)) {
  234. Serial.println("Faccia bianca");
  235. return;
  236. }
  237. }
  238. }
  239. //Serial.print("*");
  240. rec(r,g,b,offset+5);
  241. return;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement