This shows you the differences between two versions of the page.
pm:prj2023:danield:skittles-sorter [2023/05/21 21:15] dragos.gorgovan [Descriere generală] |
pm:prj2023:danield:skittles-sorter [2023/05/30 17:48] (current) dragos.gorgovan [Software Design] |
||
---|---|---|---|
Line 12: | Line 12: | ||
===== Hardware Design ===== | ===== Hardware Design ===== | ||
- | <note tip> | + | Lista de piese: |
- | Aici puneţi tot ce ţine de hardware design: | + | - placa ARDUINO UNO |
- | * listă de piese | + | - buton |
- | * scheme electrice (se pot lua şi de pe Internet şi din datasheet-uri, e.g. http://www.captain.at/electronic-atmega16-mmc-schematic.png) | + | - led RGB |
- | * diagrame de semnal | + | - senzor de miscare cu infrarosu adafruit 2168 |
- | * rezultatele simulării | + | - servomotor 360 |
- | </note> | + | - senzor de culoare TCS 230 |
+ | - fire | ||
+ | {{:pm:prj2023:danield:circuit_6969.jpg?500|}} | ||
===== Software Design ===== | ===== Software Design ===== | ||
Line 31: | Line 33: | ||
</note> | </note> | ||
+ | Biblioteci: | ||
+ | - Servo.h pentru controlul servomotorului | ||
+ | |||
+ | <hidden> | ||
+ | Cod sursa: | ||
+ | |||
+ | #include "usart.h" | ||
+ | |||
+ | #include <Servo.h> | ||
+ | |||
+ | #define BUTTON2 3 | ||
+ | |||
+ | #define BUTTON 12 | ||
+ | |||
+ | #define SERVO 11 | ||
+ | |||
+ | #define MOTION 10 | ||
+ | |||
+ | #define S0 4 | ||
+ | |||
+ | #define S2 6 | ||
+ | |||
+ | #define S3 7 | ||
+ | |||
+ | #define sensorOut 8 | ||
+ | |||
+ | #define maxColors 20 | ||
+ | |||
+ | #define noReads 10 | ||
+ | |||
+ | #define BLUE 13 | ||
+ | |||
+ | #define GREEN 9 | ||
+ | |||
+ | #define RED 5 | ||
+ | |||
+ | #define EPS 7 | ||
+ | |||
+ | struct color { | ||
+ | int r; | ||
+ | int g; | ||
+ | int b; | ||
+ | }; | ||
+ | |||
+ | Servo myservo; | ||
+ | volatile bool flag = false; | ||
+ | int redFrequency = 0; | ||
+ | int greenFrequency = 0; | ||
+ | int blueFrequency = 0; | ||
+ | color black_list[maxColors]; | ||
+ | int size = 0; | ||
+ | volatile int state = 0; | ||
+ | unsigned long last_rotation = 0; | ||
+ | int min_r = 100000, min_g = 100000, min_b = 100000; | ||
+ | int max_r = 0, max_g = 0, max_b = 0; | ||
+ | |||
+ | |||
+ | ISR (PCINT0_vect) { | ||
+ | if (!digitalRead(MOTION)) { | ||
+ | flag = true; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setMode() | ||
+ | { | ||
+ | if (state == 0) | ||
+ | state = 1; | ||
+ | } | ||
+ | |||
+ | color read_color() | ||
+ | { | ||
+ | color ret; | ||
+ | ret.r = 0; | ||
+ | ret.g = 0; | ||
+ | ret.b = 0; | ||
+ | |||
+ | for (int i = 0; i < noReads; i++) | ||
+ | { | ||
+ | digitalWrite(S2,LOW); | ||
+ | digitalWrite(S3,LOW); | ||
+ | | ||
+ | // Reading the output frequency | ||
+ | ret.r += pulseIn(sensorOut, LOW); | ||
+ | | ||
+ | // Setting GREEN (G) filtered photodiodes to be read | ||
+ | digitalWrite(S2,HIGH); | ||
+ | digitalWrite(S3,HIGH); | ||
+ | | ||
+ | // Reading the output frequency | ||
+ | ret.g += pulseIn(sensorOut, LOW); | ||
+ | | ||
+ | // Setting BLUE (B) filtered photodiodes to be read | ||
+ | digitalWrite(S2,LOW); | ||
+ | digitalWrite(S3,HIGH); | ||
+ | | ||
+ | // Reading the output frequency | ||
+ | ret.b += pulseIn(sensorOut, LOW); | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | ret.r /= noReads; | ||
+ | ret.g /= noReads; | ||
+ | ret.b /= noReads; | ||
+ | |||
+ | Serial.print("R: "); | ||
+ | Serial.println(ret.r); | ||
+ | Serial.print("G: "); | ||
+ | Serial.println(ret.g); | ||
+ | Serial.print("B: "); | ||
+ | Serial.println(ret.b); | ||
+ | |||
+ | if (ret.r > max_r) | ||
+ | max_r = ret.r; | ||
+ | |||
+ | if (ret.r < min_r) | ||
+ | min_r = ret.r; | ||
+ | | ||
+ | if (ret.g > max_g) | ||
+ | max_g = ret.g; | ||
+ | |||
+ | if (ret.g < min_g) | ||
+ | min_g = ret.g; | ||
+ | |||
+ | if (ret.b > max_b) | ||
+ | max_b = ret.b; | ||
+ | |||
+ | if (ret.b < min_b) | ||
+ | min_b = ret.b; | ||
+ | |||
+ | int redColor = map(ret.r, min_r, max_r, 255,0); | ||
+ | analogWrite(RED, redColor); | ||
+ | |||
+ | int blueColor = map(ret.b, min_b, max_b, 255,0); | ||
+ | analogWrite(BLUE, blueColor); | ||
+ | |||
+ | int greenColor = map(ret.g, min_g, max_g, 255,0); | ||
+ | analogWrite(GREEN, greenColor); | ||
+ | |||
+ | return ret; | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | USART0_init(); | ||
+ | Serial.begin(9600); | ||
+ | USART0_print("Hello\n"); | ||
+ | |||
+ | pinMode(BUTTON, 2); | ||
+ | pinMode(BUTTON2, 2); | ||
+ | pinMode(S0, OUTPUT); | ||
+ | pinMode(S2, OUTPUT); | ||
+ | pinMode(S3, OUTPUT); | ||
+ | |||
+ | pinMode(RED, OUTPUT); | ||
+ | pinMode(GREEN, OUTPUT); | ||
+ | pinMode(BLUE, OUTPUT); | ||
+ | analogWrite(RED,0); | ||
+ | analogWrite(GREEN,0); | ||
+ | analogWrite(BLUE,0); | ||
+ | |||
+ | |||
+ | pinMode(MOTION, INPUT_PULLUP); | ||
+ | |||
+ | // freq scaling 20% | ||
+ | digitalWrite(S0,HIGH); | ||
+ | |||
+ | myservo.attach(SERVO); | ||
+ | |||
+ | |||
+ | attachInterrupt(digitalPinToInterrupt(BUTTON2), setMode, FALLING); | ||
+ | PCICR |= B00000001; // We activate the interrupts of the PB port | ||
+ | PCMSK0 |= B00000100; // PB2 | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | if (flag) | ||
+ | { | ||
+ | flag = false; | ||
+ | delay(2000); | ||
+ | color temp = read_color(); | ||
+ | |||
+ | if (state == 1 && size < maxColors) | ||
+ | { | ||
+ | black_list[size].r = temp.r; | ||
+ | black_list[size].g = temp.g; | ||
+ | black_list[size].b = temp.b; | ||
+ | size++; | ||
+ | state = 0; | ||
+ | myservo.write(110); | ||
+ | delay(1200); | ||
+ | myservo.write(90); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | bool blist = false; | ||
+ | for (int i = 0; i < size; i++) | ||
+ | { | ||
+ | if ((black_list[i].r- temp.r) * (black_list[i].r- temp.r) + (black_list[i].b - temp.b) * (black_list[i].b - temp.b) | ||
+ | + (black_list[i].g- temp.g) * (black_list[i].g- temp.g) <= EPS * EPS) | ||
+ | blist = true; | ||
+ | } | ||
+ | |||
+ | if (millis() - last_rotation > 2000) | ||
+ | { | ||
+ | last_rotation = millis(); | ||
+ | |||
+ | if (blist) | ||
+ | { | ||
+ | myservo.write(110); | ||
+ | delay(1200); | ||
+ | myservo.write(90); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | myservo.write(70); | ||
+ | delay(1200); | ||
+ | myservo.write(90); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </hidden> | ||
===== Rezultate Obţinute ===== | ===== Rezultate Obţinute ===== | ||