Advertisement
LeventeDaradici

TEA5767 FM radio with rotary encoder - Arduino for beginners

Oct 2nd, 2021
1,856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <TEA5767Radio.h>
  3.  
  4. #define CLK 2
  5. #define DT 3
  6. #define SW 4
  7. TEA5767Radio radio = TEA5767Radio();
  8.  
  9. float counter = 107.7;
  10. int currentStateCLK;
  11. int lastStateCLK;
  12.  
  13. String currentDir ="";
  14. unsigned long lastButtonPress = 0;
  15.  
  16.  
  17. void setup()
  18.      {
  19.          pinMode(CLK,INPUT);
  20.            pinMode(DT,INPUT);
  21.            pinMode(SW, INPUT_PULLUP);
  22.          lastStateCLK = digitalRead(CLK);
  23.          Serial.begin(9600);
  24.            Serial.print(" Frecvency: ");
  25.            Serial.println(counter);
  26.          Wire.begin();
  27.          radio.setFrequency(107.7);
  28.      }
  29.  
  30. void loop()
  31.      {
  32.         currentStateCLK = digitalRead(CLK);
  33.           if (currentStateCLK != lastStateCLK  && currentStateCLK == 1)
  34.            {
  35.               if (digitalRead(DT) != currentStateCLK)
  36.                  {
  37.                           counter = counter + 0.1;
  38.                     if (counter > 108.1)
  39.                        {
  40.                          counter = 87.5;
  41.                        }
  42.                           currentDir ="UP";
  43.                     radio.setFrequency(counter);
  44.                      } else
  45.                       {
  46.                                 counter = counter - 0.1;
  47.                           if (counter < 87.5)
  48.                              {
  49.                                 counter = 108;
  50.                              }
  51.                           currentDir ="DOWN";
  52.                     radio.setFrequency(counter);
  53.                           }
  54.                   Serial.print("Direction: ");
  55.                   Serial.print(currentDir);
  56.                   Serial.print(" - Frecvency: ");
  57.                   Serial.println(counter);
  58.             }
  59.    
  60.   lastStateCLK = currentStateCLK;
  61.   int btnState = digitalRead(SW);
  62.     if (btnState == LOW)
  63.         {
  64.                if (millis() - lastButtonPress > 50)
  65.               {
  66.                        Serial.println("Button pressed!");
  67.                   }
  68.                lastButtonPress = millis();
  69.           }
  70.     delay(1);
  71.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement