SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino_APDS9960.h>
  2.  
  3. const int Rpins [8] = {2,3,4,5,6,7,8,9};
  4. int etuSensori;
  5. String RX;
  6.  
  7. int IRinputPin = A0;
  8. int IRinputPin2 = A1;
  9. int IRval = 0;
  10. int IRval2 = 0;
  11. int IRsendPin = 34;
  12. int IRsendPin2 = 44;
  13. volatile bool IRtosi;
  14.  
  15.  
  16.  
  17. // the setup function runs once when you press reset or power the board
  18. void setup() {
  19.  
  20.   cli(); //Poistetaan käytöstä global interruptit
  21.  
  22.   TCCR1A = 0; //Alustetaan nämä kaksi keskeistä kontrollirekisteriä
  23.   TCCR1B = 0; // ensin nollaksi
  24.  
  25.   TCCR1B |= (1 << WGM12); // Tämä bitti päälle ja timer on CTC moodissa
  26.                           // Eli tarkoittaa sitä, että timer
  27.                           // juoksee OCR1A rekisterin lukemaan asti
  28.                           // ja pyörähtää sitten nollaan, josta laskenta jatkuu
  29.                          
  30.   TCCR1B |= (1 << CS10); //Näillä kahdella bitillä B-rekisterissä
  31.   TCCR1B |= (0 << CS12); //asetetaan kellon jako 1024:lla
  32.                          
  33.   OCR1A = 210;        // Vertailurekisteriin laitetaan arvo, johon asti se timer
  34.                         // ehtii laskea yhden sekunnin aikana
  35.                         // näin saadaan yhden sekunnin keskeytystati
  36.                         // 16 000 000 Hz kello jaetaan 1024 = 15625
  37.                        
  38.  
  39.   TIMSK1 |= (1 << OCIE1A); //Timer HW:sta enabloidaan Output Compare Interrupt Enable
  40.  
  41.   sei(); //Sallitaan global interrupts
  42.  
  43.   Serial1.begin(115200);
  44.   Serial.begin(115200);
  45.     while (!Serial);
  46.  
  47.   if (!APDS.begin()) {
  48.     Serial.println("Error initializing APDS9960 sensor!");
  49.   }
  50.   for(int i = 0; i < 8; i++){
  51.     pinMode(Rpins[i], OUTPUT);
  52.   }
  53.   pinMode(IRinputPin,INPUT);
  54.   pinMode(IRsendPin,OUTPUT);
  55.   pinMode(IRinputPin2,INPUT);
  56.   pinMode(IRsendPin2,OUTPUT);
  57.  
  58.  
  59. }
  60. ISR(TIMER1_COMPA_vect){
  61.    IRtosi = !IRtosi;
  62.    digitalWrite(IRsendPin,IRtosi);
  63.    digitalWrite(IRsendPin2,IRtosi);
  64. }
  65.  
  66. // the loop function runs over and over again until power down or reset
  67. void loop() {
  68.  
  69.   IRval = analogRead(IRinputPin);
  70.   IRval2 = analogRead(IRinputPin2);
  71.   Serial.println(IRval);
  72.   Serial.println(IRval2);
  73.  
  74.   RX = Serial1.readStringUntil('\r');
  75.   int RXn = RX.toInt();
  76.  
  77.   if (APDS.proximityAvailable()) {
  78.     int proximity = APDS.readProximity();
  79.     etuSensori = proximity;
  80.     Serial.println(proximity);
  81.     // print value to the Serial Monitor
  82.     }
  83.  
  84.   Liikkuminen(RXn, etuSensori);
  85.  
  86. }
  87.  
  88. int Liikkuminen(int RXn, int fS){
  89.   if(RXn == 71 && fS > 220){
  90.     ajaEteen();
  91.     }else if(RXn == 71 && fS <= 220){
  92.       pysahdy();
  93.       }
  94.  
  95.    else if(RXn == 67){
  96.       ajaTaakse();
  97.     }
  98.    
  99.     else if(RXn == 49){
  100.       ajaVasemmalle();
  101.     }
  102.    
  103.     else if(RXn == 52){
  104.       ajaOikealle();
  105.     }else{
  106.       pysahdy();
  107.       }
  108.    
  109.   }
  110.  
  111. int ajaEteen(){
  112.       digitalWrite(Rpins[0], LOW);
  113.       digitalWrite(Rpins[1], HIGH);
  114.       digitalWrite(Rpins[2], LOW);
  115.       digitalWrite(Rpins[3], HIGH);
  116.       digitalWrite(Rpins[4], LOW);
  117.       digitalWrite(Rpins[5], HIGH);
  118.       digitalWrite(Rpins[6], LOW);
  119.       digitalWrite(Rpins[7], HIGH);
  120.   }
  121. int ajaTaakse(){
  122.       digitalWrite(Rpins[0], HIGH);
  123.       digitalWrite(Rpins[1], LOW);
  124.       digitalWrite(Rpins[2], HIGH);
  125.       digitalWrite(Rpins[3], LOW);
  126.       digitalWrite(Rpins[4], HIGH);
  127.       digitalWrite(Rpins[5], LOW);
  128.       digitalWrite(Rpins[6], HIGH);
  129.       digitalWrite(Rpins[7], LOW);
  130.   }
  131. int ajaVasemmalle(){
  132.       digitalWrite(Rpins[0], LOW);
  133.       digitalWrite(Rpins[1], HIGH);
  134.       digitalWrite(Rpins[2], HIGH);
  135.       digitalWrite(Rpins[3], LOW);
  136.       digitalWrite(Rpins[4], HIGH);
  137.       digitalWrite(Rpins[5], LOW);
  138.       digitalWrite(Rpins[6], LOW);
  139.       digitalWrite(Rpins[7], HIGH);
  140.   }
  141. int ajaOikealle(){
  142.       digitalWrite(Rpins[0], HIGH);
  143.       digitalWrite(Rpins[1], LOW);
  144.       digitalWrite(Rpins[2], LOW);
  145.       digitalWrite(Rpins[3], HIGH);
  146.       digitalWrite(Rpins[4], LOW);
  147.       digitalWrite(Rpins[5], HIGH);
  148.       digitalWrite(Rpins[6], HIGH);
  149.       digitalWrite(Rpins[7], LOW);
  150.   }
  151. int pysahdy(){
  152.       digitalWrite(Rpins[0], LOW);
  153.       digitalWrite(Rpins[1], LOW);
  154.       digitalWrite(Rpins[2], LOW);
  155.       digitalWrite(Rpins[3], LOW);
  156.       digitalWrite(Rpins[4], LOW);
  157.       digitalWrite(Rpins[5], LOW);
  158.       digitalWrite(Rpins[6], LOW);
  159.       digitalWrite(Rpins[7], LOW);
  160.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top