Advertisement
Momir

ArduinoLaserskaKomunikacijaTx

May 26th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. int vreme = 1;
  2. void setup() {
  3.   // put your setup code here, to run once:
  4.   Serial.begin(38400);
  5.   pinMode(2, OUTPUT);
  6.   cli(); // disable interrupt
  7.   TCCR0A = 0;
  8.   TCCR0B = 0;
  9.   TCNT0 = 1; // initialize the counter from 0
  10.   OCR0A = 124; // = (16*10^6) / (2000*64) - 1 (must be <256)
  11.   TCCR0A |= (1 << WGM01); // turn on CTC mode
  12.   TCCR0B |= (1 << CS01);// | (1 << CS00); // Set CS01 and CS00 bits for 64 prescaler
  13.   TIMSK0 |= (1 << OCIE0A); // enable timer compare interrupt
  14.   sei(); //allows interrupts
  15. }
  16.  
  17. ISR(TIMER0_COMPA_vect) {
  18.   vreme=0;
  19. }
  20.  
  21. //#define DELAY 1
  22. char zn;
  23. int b = 0;
  24. short niz[8];
  25.  
  26. void loop() {
  27.  
  28.   b = 0;
  29.   if (Serial.available() > 0) {
  30.  
  31.     zn = Serial.read();
  32.     if (zn != '\n') {
  33.  
  34.       for (int i = 7; i >= 0; i--)
  35.       {
  36.         if ((zn >> i) & 1)
  37.           niz[b] = 1;
  38.         else
  39.           niz[b] = 0;
  40.         b++;
  41.       }
  42.  
  43.      
  44.       digitalWrite(2, HIGH);
  45.       vreme=1;
  46.       while (vreme);
  47.  
  48.       for (int i = 0; i < b; i++) {
  49.         //Serial.print(niz[i]);
  50.  
  51.         if (niz[i] == 1)
  52.           digitalWrite(2, HIGH);
  53.  
  54.         else
  55.           digitalWrite(2, LOW);
  56.  
  57.         vreme = 1;
  58.         while (vreme);
  59.       }
  60.  
  61.       digitalWrite(2, LOW);
  62.       vreme = 1;
  63.       while (vreme);
  64.     }
  65.  
  66.     //Serial.println();
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement