Advertisement
Sergiovan

Untitled

Mar 23rd, 2015
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. class Queue {
  2.   int queue[100];
  3. public:
  4.   Queue(){
  5.     for(int i = 0; i < 100; i++){
  6.       queue[i] = 0;
  7.     }
  8.   }
  9.   bool percentage(int target, int percentage){
  10.     int per = 0;
  11.     for(int i = 0; i < 100; i++){
  12.       if(queue[i] == target){
  13.         per++;
  14.       }
  15.     }
  16.     return (per > percentage);
  17.   }
  18.   void push(int num){
  19.     for(int i = 100; i > 0; i--){
  20.       queue[i] = queue[i-1];
  21.     }
  22.     queue[0] = num;
  23.   }
  24.   void print(){
  25.     for(int i = 0; i < 100; i++){
  26.       Serial.print(queue[i]);
  27.       Serial.print(" ");
  28.     }
  29.     Serial.println();
  30.   }
  31. };
  32.  
  33. int SOUNDPIN = A0;
  34. int DISTANCEPIN = A1;
  35. Queue queue;
  36. Queue queue2;
  37. bool speek = false;
  38. bool lastspeek = false;
  39. bool yell = false;
  40. bool closeby = false;
  41. bool lastcloseby = false;
  42.  
  43. void setup() {
  44.   //Fuck
  45.   Serial.begin(9600);
  46.   queue = Queue();
  47.   queue2 = Queue();
  48. }
  49.  
  50. void loop() {
  51.   int sensor = analogRead(SOUNDPIN);
  52.   int distance = analogRead(DISTANCEPIN);
  53.  
  54.   //Serial.println(sensor);
  55.  
  56.   if(sensor < 60){
  57.     queue.push(2);
  58.   }else if (sensor < 300){
  59.     queue.push(1);
  60.   }else{
  61.     queue.push(0);
  62.   }
  63.  
  64.   if(distance > 500){
  65.     queue2.push(1);
  66.   }else{
  67.     queue2.push(0);
  68.   }
  69.  
  70.   queue.print();
  71.   if(queue.percentage(1, 2) || queue.percentage(2, 2)){
  72.     if(queue.percentage(2, 2)){
  73.       yell = true;
  74.     }else{
  75.       yell = false;
  76.     }
  77.     speek = true;
  78.   }else{
  79.     speek = false;
  80.     yell = false;
  81.   }
  82.  
  83.   if(queue2.percentage(1, 2)){
  84.     closeby = true;
  85.   }else{
  86.     closeby = false;
  87.   }
  88.  
  89.   if(closeby && !lastcloseby){
  90.     Serial.println(3);
  91.     lastcloseby = closeby;
  92.     delay(10);
  93.     return;
  94.   }
  95.  
  96.   if(closeby && lastcloseby){
  97.     lastcloseby = closeby;
  98.     delay(10);
  99.     return;
  100.   }
  101.  
  102.   if(lastcloseby && !closeby){
  103.     Serial.println(0);
  104.     lastcloseby = closeby;
  105.     delay(10);
  106.     return;
  107.   }
  108.  
  109.   if(speek && !lastspeek){
  110.     if(!yell){
  111.       Serial.println(1);
  112.     }else{
  113.       Serial.println(2);
  114.     }
  115.   }
  116.  
  117.   if((lastspeek && !speek)){
  118.     Serial.println(0);
  119.   }
  120.  
  121.   lastspeek = speek;
  122.   lastcloseby = closeby;
  123.   delay(10);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement