Hello,
here is my current code.
I reverted back to my last working version.
Hope this helps
#include <MsTimer2.h>
int switchPins[] = {38, 39, 40, 41, 42, 43, 44, 45};
int LEDpins[] = {22, 23, 24, 25, 26, 27, 28, 29};
int currentSwitchState[] = {0,0,0,0,0,0,0,0};
int AnalogValue = 0;
const int timePin1 = 8;
const int timePin2 = 9;
const int playPin = 10;
const int modePin = 11;
const int middleC = 60; // Middle C (MIDI note value 60) is the lowest note we'll play
byte note = 0x2A;
byte kanal = 0xB1;
int currentTimeState = 0;
int currentTimeStateDown = 0;
int playState = 0;
int lastplayState = 0;
int playing = 0;
int aktiv = 0;
int buttonCounter = 0;
int delaypause = 1000;
unsigned long timercount = 0;
unsigned long timer_start = 0;
unsigned long wartezeit = 5000; //1000ms = 1 sekunde
unsigned long currentTime = 250;
unsigned long Timer = 0;
int step = 0;
void setup() {
// set the states of the I/O pins:
for (int x = 0;x<8;x++) {
pinMode(LEDpins[x], OUTPUT);
pinMode(switchPins[x], INPUT);
}
pinMode(timePin1, INPUT);
pinMode(timePin2, INPUT);
pinMode(playPin, INPUT);
// Set MIDI baud rate:
Serial.begin(31250);
// turn all notes off
noteOn(0xB00, 0x7B, 0x00);
noteOn(0xB01, 0x7B, 0x00);
noteOn(0xB02, 0x7B, 0x00);
noteOn(0xB03, 0x7B, 0x00);
noteOn(0xB04, 0x7B, 0x00);
noteOn(0xB05, 0x7B, 0x00);
noteOn(0xB06, 0x7B, 0x00);
noteOn(0xB07, 0x7B, 0x00);
blink(3);
timer_start = millis();
}
void loop() {
//timing settings
currentTimeState = digitalRead(timePin1);
if (currentTimeState == 1) {
currentTime = currentTime + 50;
}
currentTimeStateDown = digitalRead(timePin2);
if (currentTimeStateDown == 1) {
if (currentTime <= 50) {
currentTime = 100;
} else {
currentTime = currentTime - 50;
}
}
//play button action
playState = digitalRead(playPin);
if (playState != lastplayState) {
// change the state of the led when someone pressed the button
if (playState == 1) {
if(playing==1) {
playing=0;
noteOn(0xB0, 0x78, 0x00);
} else {
playing=1;
}
}
// remember the current state of the button
lastplayState = playState;
}
//Main sequence
if (playing == 1) {
/* int x = 0;
for (x = 0;x<8;x++) {
currentSwitchState[x] = digitalRead(switchPins[x]);
playstep(x);
}
*/
if ((millis() - Timer) > currentTime) {
Timer = millis();
currentSwitchState[step] = digitalRead(switchPins[step]);
playstep(step);
if (step < 7) {
step++;
} else {
step = 0;
}
}
}
}
//****************************************************************
//****************************************************************
// Functions
//****************************************************************
//****************************************************************
void noteOn(byte cmd, byte data1, byte data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
// Blinks an LED 3 times
void blink(int howManyTimes){
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(LEDpins[1], HIGH);
digitalWrite(LEDpins[2], HIGH);
delay(100);
digitalWrite(LEDpins[1], LOW);
digitalWrite(LEDpins[2], LOW);
delay(100);
}
}
void playstep(int stepnr) {
if (currentSwitchState[stepnr] == 1) {
AnalogValue = analogRead(stepnr);
note = AnalogValue/8;
noteOn(0xB0, 0x7B, 0x00);
noteOn(0x90, note, 0x7F);
digitalWrite(LEDpins[stepnr], HIGH);
delay(50);
digitalWrite(LEDpins[stepnr], LOW);
}
}
void flash(int led) {
digitalWrite(LEDpins[led], HIGH);
}
void flash0(void) {
flash(0);
}
void flash1(void) {
flash(1);
}
void flash2(void) {
flash(2);
}
void flash3(void) {
flash(3);
}
void flash4(void) {
flash(4);
}
void flash5(void) {
flash(5);
}
void flash6(void) {
flash(6);
}
void flash7(void) {
flash(7);
}
greetings