SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <timer.h>
  2. #include <Keyboard.h>
  3. auto timer = timer_create_default();
  4.  
  5. int ledPin = 13;
  6. int ledr = 3;
  7. int leda = 6, ledb = 9, ledc = 10, ledd = 11;
  8. int buzz = 5;
  9. const int button = 12 ;
  10. int bstate = 0, bstate2;
  11. int aR = A1, aL = A0;
  12. int dR = 0, dL = 0;
  13. int mode = 0;
  14. int btone = 0;
  15.  
  16. void setup() {
  17.   // put your setup code here, to run once:
  18.   pinMode(ledPin, OUTPUT); pinMode(ledr, OUTPUT); pinMode(leda, OUTPUT); pinMode(ledb, OUTPUT); pinMode(ledc, OUTPUT); pinMode(ledd, OUTPUT);
  19.   pinMode(buzz, OUTPUT); pinMode(button, INPUT_PULLUP);
  20.   pinMode(aR, INPUT); pinMode(aL, INPUT);
  21.   if (Serial) {
  22.     tone(buzz, 300); delay(100); noTone(buzz); delay(300); tone(buzz, 1000); delay(300); noTone(buzz); tone(buzz, 1000); delay(300); noTone(buzz); tone(buzz, 1000); delay(300); noTone(buzz);
  23.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  24.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  25.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  26.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  27.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  28.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  29.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  30.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  31.     digitalWrite(leda, HIGH); delay(100); digitalWrite(ledb, HIGH); delay(100); digitalWrite(ledc, HIGH); delay(100); digitalWrite(ledd, HIGH); delay(100);
  32.     digitalWrite(ledd, LOW); delay(100); digitalWrite(ledc, LOW); delay(100); digitalWrite(ledb, LOW); delay(100); digitalWrite(leda, LOW);
  33.   }
  34.   digitalWrite(ledr, HIGH);
  35.   Serial.begin(9600);
  36. }
  37.  
  38. void loop() {
  39.   bstate2 = digitalRead(button);
  40.   bstate = bstate2;
  41.   buttonmode();
  42.   //=======================================================================================================================================================
  43.   dR = analogRead(aR) * 2;
  44.   dL = analogRead(aL) * 2;
  45.   handleSerial();
  46.   if (mode == 1) {
  47.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(1000);
  48.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(1000);
  49.     digitalWrite(leda, HIGH); delay(100); digitalWrite(ledb, HIGH); delay(100); digitalWrite(ledc, HIGH); delay(100); digitalWrite(ledd, HIGH); delay(100);
  50.     digitalWrite(ledd, LOW); delay(100); digitalWrite(ledc, LOW); delay(100); digitalWrite(ledb, LOW); delay(100); digitalWrite(leda, LOW); delay(100);
  51.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  52.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  53.     digitalWrite(leda, HIGH); delay(100); digitalWrite(ledb, HIGH); delay(100); digitalWrite(ledc, HIGH); delay(100); digitalWrite(ledd, HIGH); delay(100);
  54.     digitalWrite(ledd, LOW); delay(100); digitalWrite(ledc, LOW); delay(100); digitalWrite(ledb, LOW); delay(100); digitalWrite(leda, LOW); delay(100);
  55.     digitalWrite(ledr, LOW), digitalWrite(ledPin, LOW), delay(100);
  56.     digitalWrite(ledr, HIGH), digitalWrite(ledPin, HIGH), delay(100);
  57.     digitalWrite(leda, HIGH); delay(100); digitalWrite(ledb, HIGH); delay(100); digitalWrite(ledc, HIGH); delay(100); digitalWrite(ledd, HIGH); delay(100);
  58.     digitalWrite(ledd, LOW); delay(100); digitalWrite(ledc, LOW); delay(100); digitalWrite(ledb, LOW); delay(100); digitalWrite(leda, LOW); delay(100);
  59.   }
  60.   if (mode == 2) {
  61.     beat();
  62.     vum();
  63.   }
  64. }
  65.  
  66. void buttonmode()
  67. {
  68.   if (bstate == LOW)
  69.   {
  70.     if (mode == 0)
  71.     {
  72.       mode++;
  73.     }
  74.     if (mode == 1)
  75.     {
  76.       mode++;
  77.     }
  78.     if (mode == 2)
  79.     {
  80.       mode = 0;
  81.     }
  82.     btone = 1;
  83.     Serial.print("mode = "); Serial.println(mode);
  84.   }
  85.   else
  86.   {
  87.     if (btone == 1)
  88.     {
  89.       if (mode == 0)
  90.       {
  91.         tone(buzz, 1000);
  92.         delay(200);
  93.         noTone(buzz);
  94.       }
  95.       if (mode == 1)
  96.       {
  97.         tone(buzz, 1000);
  98.         delay(200);
  99.         noTone(buzz);
  100.         tone(buzz, 1000);
  101.         delay(200);
  102.         noTone(buzz);
  103.       }
  104.       if (mode == 2)
  105.       {
  106.         tone(buzz, 1000);
  107.         delay(200);
  108.         noTone(buzz);
  109.         tone(buzz, 1000);
  110.         delay(200);
  111.         noTone(buzz);
  112.         tone(buzz, 1000);
  113.         delay(200);
  114.         noTone(buzz);
  115.       }
  116.       btone = 0;
  117.     }
  118.   }
  119. }
  120.  
  121. void beat()
  122. {
  123.   Serial.print("Right = "), Serial.println(dR);
  124.   Serial.print("Left = "), Serial.println(dL);
  125.   Serial.println(" ");
  126.   int SR = map(dR, 0, 426, 0, 255);
  127.   int SL = map(dL, 0, 426, 0, 255);
  128.   if (SR == 0)
  129.   {
  130.     digitalWrite(ledr, HIGH);
  131.     digitalWrite(ledr, HIGH);
  132.   }
  133.   else
  134.   {
  135.     analogWrite(ledr, SR);
  136.   }
  137. }
  138.  
  139. void vum()
  140. {
  141.   int SR = map(dR, 0, 426, 0, 255);
  142.   if (SR == 0)
  143.   {
  144.     analogWrite(leda, 0);
  145.     analogWrite(ledb, 0);
  146.     analogWrite(ledc, 0);
  147.     analogWrite(ledd, 0);
  148.   }
  149.   else
  150.   {
  151.     analogWrite(leda, SR); delay(10);
  152.     analogWrite(ledb, SR); delay(10);
  153.     analogWrite(ledc, SR); delay(10);
  154.     analogWrite(ledd, SR); delay(10);
  155.   }
  156. }
  157.  
  158. void handleSerial()
  159. {
  160.   while (Serial.available() > 0)
  161.   {
  162.     char incomingCharacter = Serial.read();
  163.     switch (incomingCharacter)
  164.     {
  165.       case 'm':
  166.       case 'M':
  167.         {
  168.           if (mode == 1 | mode == 0)
  169.           {
  170.             Serial.println("Audio Reactive Mode");
  171.             mode = 2;
  172.           }
  173.           else
  174.           {
  175.             Serial.println("Kamu masih dalam 'Audio Reactive Mode'");
  176.           }
  177.           break;
  178.         }
  179.       case 'i':
  180.       case 'I':
  181.         {
  182.           if (mode == 2 | mode == 0)
  183.           {
  184.             Serial.println("Idle Mode");
  185.             mode = 1;
  186.           }
  187.           else
  188.           {
  189.             Serial.println("Kamu masih dalam 'Idle Mode'");
  190.           }
  191.           break;
  192.         default:
  193.           break;
  194.         }
  195.     }
  196.   }
  197. }
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
 
Top