atheistic8gmailcom

Untitled

Nov 21st, 2020
779
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // BPM control for drum machine - working 31May2020
  3.  
  4. # define BPMPotPin A0
  5.  
  6. #include <Wire.h> // Library for I2C communication
  7. #include <LiquidCrystal_I2C.h> // Library for LCD
  8.  
  9. LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); //.
  10.  
  11.  
  12. int LedPin;
  13. int TrigPin;
  14. int  BPMRead;
  15. int BPM;
  16.  
  17. void setup() {
  18.   Wire.begin();
  19.   Serial.begin(9600);
  20.   lcd.init();
  21.   lcd.backlight();
  22.  
  23.  
  24.   //configure the Led and Trigger pins as Outputs using for loop:
  25.  
  26.   for ( int LedPin = 32; LedPin < 48; LedPin++) //set 16 led pins (37 to 53)as outputs
  27.   {
  28.     pinMode (LedPin, OUTPUT);
  29.   }
  30.  
  31.   for ( int TrigPin = 2; TrigPin < 18; TrigPin++) //set 16 trigger pins (1 to 16) as outputs to level shifters
  32.  
  33.   {
  34.     pinMode (TrigPin, OUTPUT);
  35.   }
  36.  
  37.  
  38.   lcd.setCursor(0, 0);
  39.   lcd.print("| drum machine |");
  40.   lcd.setCursor(0, 1);
  41.   lcd.print("| robertronics |");
  42.  
  43.   delay(3000);
  44.   lcd.clear();
  45. }
  46.  
  47. void loop() {
  48.  
  49.  
  50.   for  (LedPin = 32, TrigPin = 2; (LedPin < 48) && (TrigPin < 18); LedPin++, TrigPin++) //counts the 16 pins for led and trigger pins
  51.   {
  52.     BPMReading(); // should really use an interrupt but for a simple sketch just reading it on every loop
  53.  
  54.     digitalWrite (LedPin, HIGH); // light LEDs in sequence matching the triggering of the audio samples by sending TrigPin LOW
  55.     digitalWrite (TrigPin, LOW);
  56.  
  57.     BPMReading();
  58.     delay (7500 / BPM); //divide the delay by 2 as it occurs twice in the function (on and off) so as to match speed to BPM (60,000 / BPM = millisecs per beat)
  59.     digitalWrite (LedPin, LOW);
  60.     digitalWrite (TrigPin, HIGH);
  61.  
  62.  
  63.     //BPMReading();
  64.     delay(7500 / BPM);
  65.     Serial.print("BPM is  ");
  66.     Serial.println(BPM);
  67.   }
  68.  
  69. }
  70.  
  71. //function to read the speed pot, map it to BPM range, write the number to the LCD and provide the BPM for the main loop:
  72.  
  73. void BPMReading()
  74.  
  75. {
  76.   BPMRead = analogRead(BPMPotPin);
  77.   BPM  = map(BPMRead, 0, 1023, 60, 800);
  78.  
  79.  
  80.   lcd.setCursor(0, 0);
  81.   lcd.print("BPM = ");
  82.   lcd.setCursor(6, 0);
  83.  
  84.   if (BPM < 100)// avoids leading zero problems for two digit BPMs
  85.   { lcd.print(" ");
  86.     lcd.print(BPM);
  87.     lcd.print(" ");
  88.   }
  89.   else {
  90.     lcd.print(BPM);
  91.   }
  92.  
  93.  
  94. }//  end of BPM reading function
RAW Paste Data