1
0
mirror of synced 2024-11-05 01:49:15 +00:00

Updated terminology in Arduino sketch

This commit is contained in:
Mark van Renswoude 2021-02-24 19:40:59 +01:00
parent 28c25c8b43
commit a1eb61a6a9
2 changed files with 43 additions and 44 deletions

View File

@ -5,10 +5,10 @@
* *
*/ */
// Set this to the number of potentiometers you have connected // Set this to the number of potentiometers you have connected
const byte KnobCount = 1; const byte AnalogInputCount = 1;
// For each potentiometer, specify the port // For each potentiometer, specify the port
const byte KnobPin[KnobCount] = { const byte AnalogInputPin[AnalogInputCount] = {
A2 A2
}; };
@ -38,10 +38,10 @@ enum OutputMode {
}; };
OutputMode outputMode = Binary; OutputMode outputMode = Binary;
byte volume[KnobCount]; byte analogValue[AnalogInputCount];
unsigned long lastChange[KnobCount]; unsigned long lastChange[AnalogInputCount];
int analogReadValue[KnobCount]; int analogReadValue[AnalogInputCount];
float emaValue[KnobCount]; float emaValue[AnalogInputCount];
unsigned long currentTime; unsigned long currentTime;
unsigned long lastPlot; unsigned long lastPlot;
@ -55,23 +55,23 @@ void setup()
// Seed the moving average // Seed the moving average
for (byte knobIndex = 0; knobIndex < KnobCount; knobIndex++) for (byte analogInputIndex = 0; analogInputIndex < AnalogInputCount; analogInputIndex++)
{ {
pinMode(KnobPin[knobIndex], INPUT); pinMode(AnalogInputPin[analogInputIndex], INPUT);
emaValue[knobIndex] = analogRead(KnobPin[knobIndex]); emaValue[analogInputIndex] = analogRead(AnalogInputPin[analogInputIndex]);
} }
for (byte seed = 1; seed < EMASeedCount - 1; seed++) for (byte seed = 1; seed < EMASeedCount - 1; seed++)
for (byte knobIndex = 0; knobIndex < KnobCount; knobIndex++) for (byte analogInputIndex = 0; analogInputIndex < AnalogInputCount; analogInputIndex++)
getVolume(knobIndex); getAnalogValue(analogInputIndex);
// Read the initial values // Read the initial values
currentTime = millis(); currentTime = millis();
for (byte knobIndex = 0; knobIndex < KnobCount; knobIndex++) for (byte analogInputIndex = 0; analogInputIndex < AnalogInputCount; analogInputIndex++)
{ {
volume[knobIndex] = getVolume(knobIndex); analogValue[analogInputIndex] = getAnalogValue(analogInputIndex);
lastChange[knobIndex] = currentTime; lastChange[analogInputIndex] = currentTime;
} }
} }
@ -86,20 +86,20 @@ void loop()
// is acceptable and saves a few calls to millis. // is acceptable and saves a few calls to millis.
currentTime = millis(); currentTime = millis();
// Check volume knobs // Check analog inputs
byte newVolume; byte newAnalogValue;
for (byte knobIndex = 0; knobIndex < KnobCount; knobIndex++) for (byte analogInputIndex = 0; analogInputIndex < AnalogInputCount; analogInputIndex++)
{ {
newVolume = getVolume(knobIndex); newAnalogValue = getAnalogValue(analogInputIndex);
if (newVolume != volume[knobIndex] && (currentTime - lastChange[knobIndex] >= MinimumInterval)) if (newAnalogValue != analogValue[analogInputIndex] && (currentTime - lastChange[analogInputIndex] >= MinimumInterval))
{ {
if (active) if (active)
// Send out new value // Send out new value
outputVolume(knobIndex, newVolume); outputAnalogValue(analogInputIndex, newAnalogValue);
volume[knobIndex] = newVolume; analogValue[analogInputIndex] = newAnalogValue;
lastChange[knobIndex] = currentTime; lastChange[analogInputIndex] = currentTime;
} }
} }
@ -170,13 +170,16 @@ void processHandshakeMessage()
{ {
case Binary: case Binary:
Serial.write('H'); Serial.write('H');
Serial.write(KnobCount); Serial.write(AnalogInputCount);
Serial.write((byte)0);
Serial.write((byte)0);
Serial.write((byte)0);
break; break;
case PlainText: case PlainText:
Serial.print("Hello! I have "); Serial.print("Hello! I have ");
Serial.print(KnobCount); Serial.print(AnalogInputCount);
Serial.println(" knobs."); Serial.println(" analog inputs and no support yet for everything else.");
break; break;
} }
@ -198,35 +201,35 @@ void processQuitMessage()
} }
byte getVolume(byte knobIndex) byte getAnalogValue(byte analogInputIndex)
{ {
analogRead(KnobPin[knobIndex]); analogRead(AnalogInputPin[analogInputIndex]);
// Give the ADC some time to stabilize // Give the ADC some time to stabilize
delay(10); delay(10);
analogReadValue[knobIndex] = analogRead(KnobPin[knobIndex]); analogReadValue[analogInputIndex] = analogRead(AnalogInputPin[analogInputIndex]);
emaValue[knobIndex] = (EMAAlpha * analogReadValue[knobIndex]) + ((1 - EMAAlpha) * emaValue[knobIndex]); emaValue[analogInputIndex] = (EMAAlpha * analogReadValue[analogInputIndex]) + ((1 - EMAAlpha) * emaValue[analogInputIndex]);
return map(emaValue[knobIndex], 0, 1023, 0, 100); return map(emaValue[analogInputIndex], 0, 1023, 0, 100);
} }
void outputVolume(byte knobIndex, byte newVolume) void outputAnalogValue(byte analogInputIndex, byte newValue)
{ {
switch (outputMode) switch (outputMode)
{ {
case Binary: case Binary:
Serial.write('V'); Serial.write('V');
Serial.write(knobIndex); Serial.write(analogInputIndex);
Serial.write(newVolume); Serial.write(newValue);
break; break;
case PlainText: case PlainText:
Serial.print("Volume #"); Serial.print("Analog value #");
Serial.print(knobIndex); Serial.print(analogInputIndex);
Serial.print(" = "); Serial.print(" = ");
Serial.println(newVolume); Serial.println(newValue);
break; break;
} }
} }
@ -234,7 +237,7 @@ void outputVolume(byte knobIndex, byte newVolume)
void outputPlotter() void outputPlotter()
{ {
for (byte i = 0; i < KnobCount; i++) for (byte i = 0; i < AnalogInputCount; i++)
{ {
if (i > 0) if (i > 0)
Serial.print('\t'); Serial.print('\t');
@ -243,7 +246,7 @@ void outputPlotter()
Serial.print('\t'); Serial.print('\t');
Serial.print(emaValue[i]); Serial.print(emaValue[i]);
Serial.print('\t'); Serial.print('\t');
Serial.print(volume[i]); Serial.print(analogValue[i]);
} }
Serial.println(); Serial.println();

View File

@ -174,13 +174,9 @@ namespace MassiveKnob.Plugin.SerialDevice.Worker
if ((char) response == 'H') if ((char) response == 'H')
{ {
// TODO support multiple I/O's specs = new DeviceSpecs(serialPort.ReadByte(), serialPort.ReadByte(), serialPort.ReadByte(), serialPort.ReadByte());
var knobCount = serialPort.ReadByte(); if (specs.AnalogInputCount > -1 && specs.DigitalInputCount > -1 && specs.AnalogOutputCount > -1 && specs.DigitalOutputCount > -1)
if (knobCount > -1)
{
specs = new DeviceSpecs(knobCount, 0, 0, 0);
return true; return true;
}
} }
else else
CheckForError(serialPort, (char)response); CheckForError(serialPort, (char)response);