1 x potentiometer
I would like to control the speed of the pattern from the EL Escudo dos. However I can't figure out why the potentiometer is not responding. My code is as follow:
const int ledPin = 13; // the number of the LED pin
//const_variable for the pot and Sound Sensor
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int time = 220; // time interval
//varibales
boolean initialise=true;
int buttonState = 0; // variable for reading the pushbutton status
int mode = 0;
int soundVal = 0;
int soundPotThresholdValue = 0;
int soundSensitivityLimit=0;
int x;
int y;
int z;
void setup()
{
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// The EL channels are on pins 2 through 9
// Initialize the pins as outputs
pinMode(2, OUTPUT); // channel A
pinMode(3, OUTPUT); // channel B
pinMode(4, OUTPUT); // channel C
pinMode(5, OUTPUT); // channel D
pinMode(6, OUTPUT); // channel E
pinMode(7, OUTPUT); // channel F
pinMode(8, OUTPUT); // channel G
pinMode(9, OUTPUT); // channel H
// We also have two status LEDs, pin 10 on the Escudo,
pinMode(10, OUTPUT);
// and ledPin(pin 13) on the Arduino itself
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(analogInPin, INPUT);
}
void loop()
//=================MODE 5(stack and unstack)==================
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
}
digitalWrite(y, LOW);
delay(time+20);
}
// =========================================
// =============== FUNCTIONS ===============
// =========================================
void detail()
{
if ((potValue != analogRead(analogInPin))) //pot is not being changed
{
time = map(potValue, 0, 1023, 0, 100);
// print the results to the serial monitor:
Serial.print("\n Potentiometer = " );
Serial.print(potValue);
Serial.print("\t Time Delay = ");
Serial.print(time);
Serial.print(" milliseconds");
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
potValue = analogRead(analogInPin);
}
}
void runAllSequences()
{
//=================MODE 5(stack and unstack)==================
for (z = 0; z <=10; z++) //because this is relatively short sequence, do it a few times times
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
}
digitalWrite(y, LOW);
delay(time+20);
}
for(x=2;x<=9;x++) //turn all channels off - reset for next loop
{
digitalWrite(x, LOW);
}
}
I apologise but I am new to coding. I have changed the runAllSequences() function to runSequence() but I still can't get the potentiometer to work. Where do I have to read the value of analogRead(analogInPin)?
I had the code with 7 patterns before and the pot was working. I have reduced the code to only 1 pattern and then the pot stopped working.
const int buttonPin = 11; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
//const_variable for the pot and Sound Sensor
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int newPotValue = 100;
int time = 220; // time interval
//varibales
boolean initialise=true;
int buttonState = 0; // variable for reading the pushbutton status
int mode = 0;
int x;
int y;
int z;
void setup()
{
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// The EL channels are on pins 2 through 9
// Initialize the pins as outputs
pinMode(2, OUTPUT); // channel A
pinMode(3, OUTPUT); // channel B
pinMode(4, OUTPUT); // channel C
pinMode(5, OUTPUT); // channel D
pinMode(6, OUTPUT); // channel E
pinMode(7, OUTPUT); // channel F
pinMode(8, OUTPUT); // channel G
pinMode(9, OUTPUT); // channel H
// We also have two status LEDs, pin 10 on the Escudo,
pinMode(10, OUTPUT);
// and ledPin(pin 13) on the Arduino itself
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(analogInPin, INPUT);
}
void loop()
//=================MODE 5(stack and unstack)==================
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
}
digitalWrite(y, LOW);
delay(time+20);
}
// =========================================
// =============== FUNCTIONS ===============
// =========================================
void detail()
{
int newPotValue = analogRead(analogInPin);
// you can print out newPotValue to see its value
if(newPotValue != potValue){
if ((potValue != analogRead(analogInPin))) //pot is not being changed
{
time = map(potValue, 0, 1023, 0, 100);
// print the results to the serial monitor:
Serial.print("\n Potentiometer = " );
Serial.print(potValue);
Serial.print("\t Time Delay = ");
Serial.print(time);
Serial.print(" milliseconds");
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
potValue = newPotValue;
potValue = analogRead(analogInPin);
}
}
}
void runAllSequence()
{
//=================MODE 5(stack and unstack)==================
for (z = 0; z <=10; z++) //because this is relatively short sequence, do it a few times times
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
}
digitalWrite(y, LOW);
delay(time+20);
}
for(x=2;x<=9;x++) //turn all channels off - reset for next loop
{
digitalWrite(x, LOW);
}
}
The potentiometer is only read in detail(). The function detail() is only called from runAllSequence(). But runAllSequence() is never called from anywhere. So it never runs. So detail() never runs. So the potentiometer is never read.
You need to think more clearly about what is supposed to happen and when.
Hi Steve,
I realised by adding detail() after the void loop has activated the potentiometer. However when turning the knob, the speed of he pattern goes too fast (delay tiem range from 220 - 0). Would you knoe how to change the range of speed of the pattern (delay time range from 750 - 50)?
Hope this makes sense. Thank you
Please find the updated code below:
const int ledPin = 13; // the number of the LED pin
//const_variable for the pot and Sound Sensor
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogInPinSound = A2; // Analog input Sound Sensor
int potValue = 0; // value read from the pot
int newPotValue = 100;
int time = 220; // time interval
//varibales
boolean initialise=true;
int buttonState = 0; // variable for reading the pushbutton status
int mode = 0;
int soundVal = 0;
int soundPotThresholdValue = 0;
int soundSensitivityLimit=0;
int x;
int y;
int z;
void setup()
{
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// The EL channels are on pins 2 through 9
// Initialize the pins as outputs
pinMode(2, OUTPUT); // channel A
pinMode(3, OUTPUT); // channel B
pinMode(4, OUTPUT); // channel C
pinMode(5, OUTPUT); // channel D
pinMode(6, OUTPUT); // channel E
pinMode(7, OUTPUT); // channel F
pinMode(8, OUTPUT); // channel G
pinMode(9, OUTPUT); // channel H
// We also have two status LEDs, pin 10 on the Escudo,
pinMode(10, OUTPUT);
// and ledPin(pin 13) on the Arduino itself
pinMode(ledPin, OUTPUT);
pinMode(analogInPin, INPUT);
}
void loop()
{
detail();
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
}
}
digitalWrite(y, LOW);
delay(time+20);
}
}
// =============== FUNCTIONS ===============
void detail()
{
int newPotValue = analogRead(analogInPin);
// you can print out newPotValue to see its value
if(newPotValue != potValue){
{
time = map(potValue, 0, 1023, 0, 100);
// print the results to the serial monitor:
Serial.print("\n Potentiometer = " );
Serial.print(potValue);
Serial.print("\t Time Delay = ");
Serial.print(time);
Serial.print(" milliseconds");
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
potValue = newPotValue;
potValue = analogRead(analogInPin);
}
}
}
void runAllSequence()
{
for (z = 0; z <=10; z++) //because this is relatively short sequence, do it a few times times
{
for (x = 2; x <= 9; x++)
{
for (y = 9; y >= x; y--)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
digitalWrite(x, HIGH);
}
for (x = 9; x >= 2; x--)
{
for (y = x; y <= 9; y++)
{
digitalWrite(y, HIGH);
delay(time);
digitalWrite(y, LOW);
detail();
}
}
digitalWrite(y, LOW);
delay(time+20);
}
for(x=2;x<=9;x++) //turn all channels off - reset for next loop
{
digitalWrite(x, LOW);
}
}
The variable you are talking about is 'time'. It starts at 220 but then it is set to a value between 0 to 100 by the map() statement in detail(). If you understand what map() is doing you'll see you just need to change the 0, 100 to the values you prefer, perhaps 50, 750 but you can play with those values until it performs as you want.