Advertisement
Guest User

arduino 2 analog channels

a guest
Apr 30th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include "pitches.h"
  2. #include <SPI.h>
  3. #include <SD.h>
  4. #include <TMRpcm.h>
  5.  
  6. const int buttonPin1 = A2; //serie 1 (eerste 5 knoppen)
  7. const int buttonPin2 = A3; //serie 2 (tweede 5 knoppen)
  8. File myFile;
  9. const int chipSelect = 4; //pin number sd card
  10. TMRpcm tmrpcm; //WAV afspeel object uit TMRpcm library
  11. const int pinSpeaker = 9;
  12. const int delayTijd = 100; //delaytime
  13.  
  14.  
  15. void setup() {
  16.     //initilize buttons
  17.     pinMode(buttonPin1, INPUT_PULLUP);
  18.     pinMode(buttonPin2, INPUT_PULLUP);
  19.     Serial.begin(9600);
  20.     pinMode(SS, OUTPUT);
  21.    
  22.     //initialize SD card connection
  23.     if (!SD.begin(chipSelect)) {
  24.         Serial.println("initialization SD card failed!");
  25.         return;
  26.     }
  27.    
  28.     Serial.println("initialization SD card done.");
  29. }
  30.  
  31.  
  32. void loop() {
  33.     delay(delayTijd);
  34.     // read the state of the switch into a local variable:
  35.     int readingPin1 = analogRead(buttonPin1);
  36.     //delay(20);
  37.     int readingPin2 = analogRead(buttonPin2);
  38.    
  39.     //print analog values
  40.     Serial.println(readingPin1);
  41.     Serial.println(readingPin2);
  42.    
  43.     //KNOP 1
  44.     if ((readingPin1 >= 956)&&(readingPin1 <= 957)){
  45.         Serial.println("knop 1");
  46.     playwav();
  47.     }
  48.    
  49.     //KNOP 2
  50.     else if ((readingPin1 >= 602)&&(readingPin1 <= 604)){
  51.         Serial.println(" knop 2");
  52.     playwav();
  53.     }
  54.    
  55.     //KNOP 3
  56.     else if ((readingPin2 >= 968)&&(readingPin2 <= 970)){
  57.         Serial.println(" knop 3");
  58.     playwav();
  59.     }
  60.  
  61.     //KNOP 4
  62.     else if ((readingPin1 >= 329)&&(readingPin1 <= 330)){
  63.         Serial.println(" knop 4");
  64.     playwav();
  65.     }
  66.  
  67.     //KNOP 5
  68.     else if ((readingPin2 >= 849)&&(readingPin2 <= 853)){
  69.         Serial.println(" knop 5");
  70.     playwav();
  71.        
  72.     }
  73.     //KNOP 6
  74.     else if ((readingPin2 >= 897)&&(readingPin2 <= 899)){
  75.         Serial.println(" knop 6");
  76.     playwav();
  77.     }
  78.     //KNOP 7
  79.     else if ((readingPin2 >= 715)&&(readingPin2 <= 717)){
  80.         Serial.println(" knop 7");
  81.     playwav();
  82.     }      
  83.     //KNOP 8
  84.     else if ((readingPin1 >= 523)&&(readingPin1 <= 526)){
  85.         Serial.println(" knop 8");
  86.     playwav();
  87.     }
  88.     //KNOP 9
  89.     else if ((readingPin1 >= 980)&&(readingPin1 <= 982)){
  90.         Serial.println(" knop 9");
  91.     playwav();
  92.     }
  93.     //KNOP 10
  94.     else if ((readingPin2 >= 933)&&(readingPin2 <= 936)){
  95.         Serial.println(" knop 10");
  96.     playwav();
  97.     };
  98. }
  99.  
  100.  
  101.  
  102. //Plays a .WAV file from the SD card
  103. void playWav(char* filename) {
  104.     if (tmrpcm.isPlaying() != 1) {
  105.         tmrpcm.speakerPin = pinSpeaker;
  106.         pinMode(10, OUTPUT);
  107.         digitalWrite(10, HIGH);
  108.         pinMode(pinSpeaker,OUTPUT);
  109.        
  110.         Serial.println("Reading WAV from SD CARD = ok");
  111.         Serial.println("Playing WAV:");
  112.         tmrpcm.setVolume(6);
  113.         Serial.println(filename);
  114.         tmrpcm.play(filename);
  115.         while (tmrpcm.isPlaying() == 1) {
  116.             delay(50);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement