Hello!
Currently I am working on a step sequencer with a touch screen and an audio shield. I am trying to create arrays to make my code a little bit simpler. For some reason though, the first step does not work, no matter what I do. Also the fourth one's LED is on all the time, as soon as I press on the step..
My code:
#include <Arduino.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Encoder.h>
#include <ILI9341_t3.h>
#include <font_Arial.h> // from ILI9341_t3
#include <XPT2046_Touchscreen.h>
#define CS_PIN 8
#define TFT_DC 9
#define TFT_CS 10
XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN 2
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);
namespace modulo4
{
float map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
} // namespace modulo4
int width = 2;
constexpr int arrayLength = 257; // const kannst du während des programms initialisieren --> constexp liegt schon von Anfang an fest
float wave1ValuesUnscaled[arrayLength] = {0};
float wave2ValuesUnscaled[arrayLength] = {0};
int X, Y;
int Y_old;
int X_old;
boolean previous_is_touched;
typedef struct
{
int x;
int y;
} points_t;
AudioSynthWaveform *waveform = new AudioSynthWaveform[4];
AudioEffectEnvelope *envelope = new AudioEffectEnvelope[4];
int b1 = 0;
int b2 = 0;
int b3 = 0;
int b4 = 0;
float defaultAttackValue = {50};
float defaultDecayValue = {200};
float defaultSustainValue = {200};
float defaultReleaseValue = {200};
float attack[4] = {defaultAttackValue};
float decay[4] = {defaultDecayValue};
float sustain[4] = {defaultSustainValue};
float release[4] = {defaultReleaseValue};
/* int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6; */
int frequency1 = {500};
int frequency2 = {700};
int frequency3 = {900};
int frequency4 = (1100);
int stepLED[] = {3, 4, 5, 6};
float amplitude[] = {0.3};
int frequency[] = {frequency1, frequency2, frequency3, frequency4};
float mixerAmplitude = 0.1;
int StepState[] = {1};
unsigned long millisDelay = 400;
float yTop = 0;
float yBottom = 240;
int xLeft = 0;
int xRight = 320;
int xLeft256 = 0;
int xRight256 = 256;
int squareLength256 = 59;
int gap256 = 4;
float squareHeight = 14;
int squareLength = 73;
int radiusOfRoundCorner = 3;
int gap = 5;
char buttonNames[4][10] = { //First bracket indicates the amount of words
"Clear", // second bracket indicates the amount of maximal letters for one string
"<----",
"---->",
"Waves"
};
int m_step = 0;
unsigned long m_interval = 300;
int m_STEPNUM = 4; // Amount of steps
unsigned long m_lastMillis;
int m_StepState[4] = {0};
int bpm = 120;
int previousA2 = 0; // previously sent poti values, to detect changes
int minBPM = 30;
int maxBPM = 220;
const int numReadings = 5; // Anzahl der Readings
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A1; // Analog input
int average_bpm = 0;
int average_bpm_alt = 0;
int16_t wave1ValuesScaled[arrayLength] = {0};
int16_t wave2ValuesScaled[arrayLength] = {0};
boolean wastouched = true;
void redrawButtons()
{
tft.fillRoundRect(0, 224, 76, 14, 3, CL(0, 0, 0)); // Black background
for (int i = 0; i < 4; ++i)
{ // DRAW THE FOUR SQUARES AT THE BOTTOM
tft.fillRoundRect((gap + xLeft + ((gap + squareLength) * i)), (yBottom - squareHeight), squareLength, squareHeight, radiusOfRoundCorner, CL(250, 0, 0));
tft.setCursor(26 + ((gap + squareLength) * i), 229);
tft.print(buttonNames[i]);
}
}
void ADSR(float att, float dec, float sus, float rel)
{ //HIGH and LOW are not boolean values.. they are integers, where HIGH = 1 and LOW = 0.
// b1 = digitalRead(20);
b2 = digitalRead(2);
b3 = digitalRead(3);
b4 = digitalRead(4);
for (int i{0}; i < 4; ++i)
{
envelope[i].attack(attack[i]);
envelope[i].decay(decay[i]);
envelope[i].sustain(sustain[i]);
envelope[i].release(release[i]);
}
void clearDisplay()
{
tft.fillScreen(ILI9341_BLACK);
redrawButtons();
tft.drawLine(0, 120, 320, 120, CL(255, 255, 255));
tft.drawLine(0, 121, 320, 121, CL(255, 255, 255));
tft.drawLine(0, 119, 320, 119, CL(255, 255, 255));
}
void clearArray()
{
for (X = 0; X < arrayLength; X++)
{
wave1ValuesUnscaled[X] = 0;
}
}
boolean pointInRect(int x, float y, float rectX, float rectY, float rectW, float rectH)
{
boolean ret = false;
if ((x >= rectX) && (x <= (rectX + rectW)) && (y >= rectY) && (y <= (rectY + rectH)))
{
ret = true;
}
return ret;
}
void setup()
{
Serial.begin(38600); // 38600 symbols per second
for (int i{0}; i < 4; ++i)
{
pinMode(stepLED[i], OUTPUT);
}
int stepButtonPin[] = {0, 1, 14, 15};
for (int i{0}; i < 4; ++i)
{
pinMode(stepButtonPin[i], INPUT_PULLUP);
}
for (int i{0}; i < 4; ++i)
{
attack[i] = defaultAttackValue;
decay[i] = defaultDecayValue;
sustain[i] = defaultSustainValue;
release[i] = defaultReleaseValue;
}
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
for (int i{0}; i < 4; ++i)
{
mixer1.gain(i, mixerAmplitude);
}
for (int i{0}; i < 4; ++i)
{
waveform[i].begin(0); // WAVEFORM_SINE expands to 0
waveform[i].amplitude(amplitude[i]);
waveform[i].frequency(frequency[i]);
}
/* waveform1.begin(WAVEFORM_SINE); // WAVE 1
waveform1.amplitude(amp1);
waveform1.frequency(freq1);
waveform2.begin(WAVEFORM_SINE); // WAVE 2
waveform2.amplitude(amp2);
waveform2.frequency(freq2);
waveform3.begin(WAVEFORM_SINE); // WAVE 3
waveform3.amplitude(amp3);
waveform3.frequency(freq3);
waveform4.begin(WAVEFORM_SINE); // WAVE 4
waveform4.amplitude(amp4);
waveform4.frequency(freq4); */
//----------Touch---------//
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.drawLine(0, 0, 320, 120, CL(255, 255, 255));
// tft.drawLine(0, 121, 320, 121, CL(255, 255, 255));
// tft.drawLine(0, 119, 320, 119, CL(255, 255, 255));
ts.begin();
ts.setRotation(1);
while (!Serial && (millis() <= 1000))
; // weiß nicht was das ist
clearDisplay();
redrawButtons();
}
void loop()
{
/*int att = analogRead(A1);
int dec = analogRead(A0);
int rel = analogRead(A4);
int sus = analogRead(A3); */
static float bpm = {120}; // wird wegen static nur einmal initialisiert
int n1 = analogRead(A1);
boolean istouched = ts.touched();
int X;
float Y;
if (istouched)
{
TS_Point p = ts.getPoint(); // point getter function
X = map(p.x - 320, 3482, 0, 0, arrayLength - 1);
if (X < 0)
{
X = 0;
}
Y = modulo4::map(p.y - 240.0, 3465.0, 0.0, 1.0, -1.0);
wave1ValuesUnscaled[X] = Y;
clearDisplay(); // Refreshing screen while drawing to remove values where one X has two Y points
Y = map(Y, -1.0, 1.0, 240.0, 0.0);
if (previous_is_touched)
{
for (int i = X_old; i < X;)
{
if (X_old <= X)
{
i++;
}
else
{
i--;
}
wave1ValuesUnscaled[i] = modulo4::map(i, X_old, X, wave1ValuesUnscaled[X_old], wave1ValuesUnscaled[X]);
}
}
X_old = X;
if (X > arrayLength)
{ // Avoiding glitchy values below X=257
X = 257;
}
}
previous_is_touched = istouched;
int digitalReadValues[] = {digitalRead(0), digitalRead(1), digitalRead(14), digitalRead(15)};
if ((millis() - m_lastMillis) > m_interval)
{
for (int i{0}; i < 4; ++i)
{
// m_step = i;
if (digitalReadValues[i] == LOW) // gedrückt
{
if (m_StepState[i] == 0)
{
m_StepState[i] = 1;
}
else
{
m_StepState[i] = 0;
}
}
if ((m_step) == i)
{
digitalWrite(stepLED[i], m_StepState[i]);
digitalWrite(stepLED[i] - 1, LOW);
envelope[i].noteOn();
waveform[i].amplitude(m_StepState[i]);
waveform[i].frequency(frequency[i]);
/* envelope1.noteOn();
waveform1.amplitude(m_StepState[i]);
waveform1.frequency(newEnc1); */
}
}
if (m_step == m_STEPNUM)
{
m_step = 0;
}
m_step++;
m_lastMillis = millis();
}
}