Post the sketch that doesn't work.
// Turntable Pins
int turntableOne = 0;
int turntableTwo = 1;
float x=0;
float y=0;
char databyte1=0x00;
char databyte2=0x40;
int a=0;
int currpos[3] = { 0xE0, 0x00, 0x40 };
// Stores the digital values
int digA = 0;
int digB = 0;
// Stores the last four 'patterns'
int prevPatterns[4] = {0, 0, 0, 0};
// Stores the current 'pattern'
int currPattern = 0;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(115200);
}
void loop() {
digA = digitalRead(2);
digB = digitalRead(3);
if (digA && !digB) {
// Only A is on
currPattern = 1;
} else if (!digA && digB) {
// Only B is on
currPattern = 3;
} else if (digA && digB) {
// Both A and B are on
currPattern = 2;
} else if (!digA && !digB) {
// Neither A nor B are on
currPattern = 4;
}
if (currPattern != prevPatterns[0]) {
// The current pattern is different to the last one, and is not just a repetition.
// Shift patterns up, where prevPatterns[0] is the newest
for (int i = 3; i > 0; i--) {
prevPatterns[i] = prevPatterns[i - 1];
}
prevPatterns[0] = currPattern;
if (arrayMatches(prevPatterns, 1, 2, 3, 4) || arrayMatches(prevPatterns, 2, 3, 4, 1) || arrayMatches(prevPatterns, 3, 4, 1, 2) || arrayMatches(prevPatterns, 4, 1, 2, 3))
{
// Direction A (clockwise)
//Serial.print(">");
x+=0.5;
if (x==1) { x=0;
if (a==0) { currpos[1] = 0x00; currpos[2] = 0x40; a=1; }
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
if (currpos[2]==127)
{
if (currpos[1]!=127) { currpos[2]=0x7F; midiCC(0xE0, currpos[1]+=1, currpos[2]); }
else { currpos[1]=0x00; currpos[2]=0x40; }
}
else if (currpos[2]!=127) midiCC(0xE0, currpos[1], currpos[2]+=1); }
}
else if (arrayMatches(prevPatterns, 3, 2, 1, 4) || arrayMatches(prevPatterns, 2, 1, 4, 3) || arrayMatches(prevPatterns, 1, 4, 3, 2) || arrayMatches(prevPatterns, 4, 3, 2, 1))
{
// Direction B (anticlockwise)
//Serial.print("<");
y+=0.5;
if (y==1) { y=0;
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
if (a==1) { a=0; currpos[1] = 0x00; currpos[2] = 0x40; }
if (currpos[2]!=0) { currpos[1]=0x7F; midiCC (0xE0, currpos[1], currpos[2]-=1); }
else {
if (currpos[1]!=0) { currpos[2]=0; midiCC (0xE0, currpos[1]-=1, 0x00); }
else { currpos[1]=0x00; currpos[2]=0x40; }
}
} }
}
}
int arrayMatches(int array[], int a, int b, int c, int d) {
// If a, b, c & d match array items 0 to 3, then return true.
return (array[0] == a && array[1] == b && array[2] == c && array[3] == d);
}
void midiCC(char CC_data, char c_num, char c_val){
Serial.print(CC_data, BYTE);
Serial.print(c_num, BYTE);
Serial.print(c_val, BYTE);
}
I'm not sure what are you trying to ask me, when I upload the Blink sketch, it works as expected, I turn off the board, power it again, and the LED blinks again.
When the bootloader runs, it blinks the LED. If there is no Sketch loaded (Flash was erased), the bootloader blinks the LED forever. After a power cycle, Is the LED blinking because the Sketch is blinking the LED or because the bootloader is blinking the LED? An easy way to tell is to upload an empty Sketch, observe the board's behaviour after pressing the reset button, cycle the power, and observe the board's behaviour after pressing the reset button.
When I upload empty sketch, the board blinks 3 times. Same thing after I press reset and same thing after power cyle and reset. With the Blink sketch, it blinks infinitely.
It's advisable to update the MEGA8U2 usb-to-serial firmware and the m328p bootloader on a UNO board; the first release came with bugs on both chips.
How do I do that?