Hello,
I am currently making a code for a spotify remote which goes to the next song when you turn a triangle right and goes to the previous song when you turn the triangle left, I use LDR's as sensors on each side of the triangle. This is my code:
const int sensorPinA = A0;
const int sensorPinB = A1;
const int sensorPinC = A2;
const int sensorPinD = A3;
int sensorValueA = 0;
int sensorValueB = 0;
int sensorValueC = 0;
int sensorValueD = 0;
long registration1 = 0;
long registration2 = 2000;
int next = 1;
int previous = 2;
int playpause = 3;
int v = 40;
void setup() {
Serial.begin(9600);
}
void loop() {
//Volgende
//Draaiing van vlak A naar vlak B
if(sensorValueA < v && sensorValueB > v && sensorValueC > v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA > v && sensorValueB < v && sensorValueC > v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(next);
registration2 = 2000;
registration1 = 0;
}
//Draaiing van vlak B naar vlak C
if(sensorValueA > v && sensorValueB < v && sensorValueC > v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA > v && sensorValueB > v && sensorValueC < v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(next);
registration2 = 2000;
registration1 = 0;
}
//Draaiing van vlak C naar vlak A
if(sensorValueA > v && sensorValueB > v && sensorValueC < v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA < v && sensorValueB > v && sensorValueC > v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(next);
registration2 = 2000;
registration1 = 0;
}
//Vorige
//Draaiing van vlak A naar vlak C
if(sensorValueA < v && sensorValueB > v && sensorValueC > v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA > v && sensorValueB > v && sensorValueC < v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(previous);
}
registration2 = 2000;
registration1 = 0;
//Draaiing van vlak C naar vlak B
if(sensorValueA > v && sensorValueB > v && sensorValueC < v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA > v && sensorValueB < v && sensorValueC > v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(previous);
}
registration2 = 2000;
registration1 = 0;
//Draaiing van vlak B naar vlak A
if(sensorValueA > v && sensorValueB < v && sensorValueC > v){
registration1 = millis();
delay(500);
}
sensorValueA = analogRead(sensorPinA);
sensorValueB = analogRead(sensorPinB);
sensorValueC = analogRead(sensorPinC);
if(sensorValueA < v && sensorValueB > v && sensorValueC > v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(previous);
}
registration2 = 2000;
registration1 = 0;
//Rechtop zetten voor play/pause
if (sensorValueD >v){
registration1 = millis();
delay(500);
}
sensorValueD = analogRead(sensorPinD);
if (sensorValueD < v){
registration2 = millis();
}
if (registration2 - registration1 <= 1500 && registration2 - registration1 >= 0){
Serial.print(playpause);
}
}
I have linked the serial print to a processing code which then sends a command to spotify (this works fine). The problem however is that the arduino keeps on sending the next or previous command every 500 milliseconds, even when the triangle is not moving, so the song keep skipping through. Can you guys help me because I can't figure out what the problem is...
Thanks in advance