'Muzikaal' project

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 1
Geregistreerd: 05 Aug 2022, 14:44

'Muzikaal' project

Berichtdoor JornP » 05 Aug 2022, 14:55

Hey iedereen.

Ik ben bezig met een Arduino UNO project. Het moet iets 'muzikaals' zijn (moet geluid maken) en het heeft deze vereisten:
- At least 2 different sensor Inputs (light, temp, pressure, IR)
- an LCD display, controlled by buttons and or rotary encoder
- 1 DC motor/pump, powered by an external power supply (battery, wall socket adapter )
- 2 extra outputs (LED, AUDIO,buzzer, ...)

Ik had eerst een piezo buzzer bediend met knop. Op het LCD scherm kon je via een rotary encoder je noot kiezen(bv.: A#, B, ...). Dit werkt maar voldoet natuurlijk nog niet aan alle vereisten.

Zou iemand me kunnen helpen met de andere benodigdheden? Of weet iemand wat ik kan doen ipv. wat ik nu heb?
Alle hulp is welkom!



Code: Alles selecteren
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,8,7,6,5);
#define piezoPin 10

 
const int DISPLAY_SHOW_KEY = 0; // 3 example states (adapt to your elements you would like to show, add if you will but also change the number in DISPLAY_ITEMS
const int DISPLAY_SHOW_TEMP = 1;
const int DISPLAY_SHOW_NOTE = 2;
const int DISPLAY_ITEMS = 3; // the number of states made above, change if you have more or less DISPLAY_SHOW_ITEMS

const int DISPLAY_SET_TIMEINTERVAL = 10; //make sure display_set states > DISPLAY_ITEMS
const int DISPLAY_SET_MINTEMP = 11;
const int DISPLAY_SET_BASENOTE = 21;

String RootKey[12] = { "C" , "C#" , "D", "D#", "E", "F" , "F#", "G", "G#", "A", "A#", "B" };
String KeyMode[2] = {"Major", "minor"};
int tonePitch[19] = {262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740}; //this array stores the pitches of the notes; it has size 19 so it can reach the thirds and fifths of the B scales required to play the BM and Bm triads.

//int toneLength = 500;
//int songLength = 19;

//bool play = true;
unsigned long startPlay = 0;
int playNumb = 0;

unsigned int keyPosition = 504;
int modulatedKey= keyPosition%12;
int modePosition= 0;
int modulatedMode=modePosition%2;

String currentkey = RootKey[modulatedKey];
String currentMode = KeyMode[modePosition];

int smoothSignal[10]= {0,0,0,0,0,0,0,0,0,0};
int counter= 0;
int smoothPitch = 0;
#define pressurePin A0

//rotary encoder
const int encoder1Pin1 = 2;
const int encoder1Pin2 = 3;
#define encoderPin3 4
static uint8_t prevNextCode = 0;
static uint16_t store = 0;
unsigned int byte6_7 = 0;
long lastRotPoll = 0;
long lastRotPushCheck = 0;
long rotPushCheckInterval= 0;
//variable keeping current Display state
int displayState = 0;
long pressureValue=0;
//value variables for settings (adapt to your settings)...


//for refreshing the screen every 1000 milliseconds (even when nothing changes)
unsigned long lastRefreshScreen;
int refreshScreenInterval = 1000;
void setup()
{
     Serial.begin(9600);
     lcd.begin(16,2);
     lcd.clear();
     lcd.print(currentkey+" "+currentMode);
    displayState = DISPLAY_SHOW_KEY; //set displaystate to first state
    lastRefreshScreen = millis();

    //set rot enc pins
    pinMode(encoder1Pin1, INPUT_PULLUP); 
    pinMode(encoder1Pin2, INPUT_PULLUP);

    pinMode(pressurePin, INPUT);
    pinMode(piezoPin, OUTPUT);
    pinMode(encoderPin3,INPUT_PULLUP);
   
    //int sample= analogRead(encoderPin3);
 
}

// put your main code here, to run repeatedly:


void loop()
{
  smoothSignal[counter%10]=pressureValue;
  smoothPitch=(smoothSignal[0]+smoothSignal[1]+smoothSignal[2]+smoothSignal[3]+smoothSignal[4]+smoothSignal[5]+smoothSignal[6]+smoothSignal[7]+smoothSignal[8]+smoothSignal[9])/10;
  Serial.println(smoothPitch+" "+analogRead(pressurePin));
  //if(analogRead(pressurePin)>0)
  {
  if(analogRead(pressurePin) > 0) {
    //playSong();
    Serial.println("thing touched");
    tone(piezoPin,tonePitch[modulatedKey],500);  //getal is lengte dat noot wordt afgespeeld
  }
 
  //handle the input
  handleRotaryInput();
  //counter++;
  if(millis()>lastRefreshScreen+refreshScreenInterval)
  {
    update();
  }
 // if (play){
  //  playSong();
  }
}


void update()
{
    lcd.clear();
    lcd.print(currentkey+" "+currentMode);
    lastRefreshScreen = millis();
}

void handleRotaryInput()//change the serial read code to rotary encoder left right and push
{

  char action = ' '; //contains nothing, right or left turn
  static int8_t c,val;
  int rotDepressPin = 5;

//code to read rot encoder left/right turn DO NOT CHANGE
  if(millis() >= lastRotPoll + 20)
  {
    byte6_7 = 0;
  }

  bool doReset = false;
  if( val=read_rotary(encoder1Pin1, encoder1Pin2) )
  {
    c +=val;
    lastRotPoll = millis();

    action  = 'l'; //left turn
   
    if (val > 0) //
    {
      action  = 'r'; //right turn
    }
  }//end code for rot encoder

  val = digitalRead(encoderPin3);
  //Serial.println(val);
  if (val == LOW && (millis() > lastRotPushCheck + rotPushCheckInterval))
  {
    action = 'p';
    lastRotPushCheck = millis();
  }
    switch(action)
    {
      case 'r':
      {
        Serial.println("right turn");
        rotEncTurned(true);
        break;
      }
      case 'l':
      {
        Serial.println("left turn");
        rotEncTurned(false);
        break;
      }
      case 'p':
      {
        Serial.println("pressed");
        rotEncPressed();
        break;
      }
    }
}

void rotEncPressed()//handle presses : if in temp mode => goto set temp, when in set temp mode go back to show temp and similar for others
{
        modePosition = modePosition+1;
        modulatedMode= modePosition%2;
        currentMode = KeyMode[modulatedMode];
       // Serial.println(currentkey+" "+currentMode);
        update();
}

void rotEncTurned(bool turningRight) // use the bool true when a right rotate has been detected, false if left was detected
{
  if(turningRight)// when right turn detected: go to the next menu item (when at last go back to first)
  {
    keyPosition = keyPosition+1;
    modulatedKey= keyPosition%12;
    currentkey = RootKey[modulatedKey];
    //Serial.println(currentkey+" "+currentMode);
    update();
  }
  else // go to previous show item when < 0 go back to last
  {
    keyPosition = keyPosition-1;
    modulatedKey= keyPosition%12;
    currentkey = RootKey[modulatedKey];
    update();
  }
}
   
 /* void playSong(){
  if ((millis() > startPlay + toneLength * playNumb) && play)
   {
    playNumb++;
   }
   if (playNumb < songLength)
   {
    tone(piezoPin,tonePitch [playNumb]) ;
   }
  else
  {
    noTone(piezoPin);
  }
  }
*/


// A vald CW or  CCW move returns 1, invalid returns 0.
// made by the brilliant David Van Hoecke
int8_t read_rotary(int pin1, int pin2) {
  static int8_t rot_enc_table[] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0};
  prevNextCode <<= 2;
  if (digitalRead(pin1)) prevNextCode |= 0x02;
  if (digitalRead(pin2)) prevNextCode |= 0x01;
  prevNextCode &= 0x0f;

   // If valid then store as 16 bit data.
   if  (rot_enc_table[prevNextCode] ) {
      store <<= 4;
      store |= prevNextCode;
      if ((store&0xff)==0x2b) return -1;
      if ((store&0xff)==0x17) return 1;
   }
   return 0;
}

Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: 'Muzikaal' project

Berichtdoor shooter » 08 Aug 2022, 14:58

zet alle tijd eerst eens naar unsigned long zodat ze allemaal hetzelfde zijn, doe een - teken gebruiken in je timing dat is beter zie voorbeeld tikken without delay daar staat ook waarom.
er staan een paar foutjes in de code als ik compileer
en ook voor jou kom naar de makerfaire in eindhoven of een keer naar de arduino bijeenkomst in leiderdorp.
en bedenk wat leuks met een piep als je achetruit gaat en een brom als je vooruitgaat bijvoorbeeld
paul deelen
shooter@home.nl

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 10 gasten