LED Sequences in Arduino
For my first practice back into the Arduino I have a circuit that counts up to each pin in progression as well as one that changes circuit based on voltage.
Script
The code above is simplified by using a for loop between whatever int I set as the min and max range so I can easily set up every pint 3-13 by simply changing high pin to 13 and setting up the respective circuit.
//Progressive LED timer
//timer
float counter=0;
float maxCounter = 5000;
//pin range
int lowPin = 3;
int highPin = 6;
void setup() {
//enable pins in range
for (int a = lowPin; a <= highPin; a ++)
{
pinMode(a, OUTPUT);
}
//Serial.begin(9600);
//Serial.println("Hello World");
//pinMode(7, OUTPUT);
}
void loop() {
//for each pin enabled
for (int a = lowPin; a <= highPin; a++)
{
//maps a range that will activate each pin in progression
//generates a decimal high and low for each pin to activate at dependent on its index.
if (counter <= (maxCounter/(highPin-lowPin)) * (a-lowPin+1) &&
counter >= (maxCounter/(highPin-lowPin)) * (a-lowPin))
{
digitalWrite(a, HIGH);
}
//deactivates pin when its out of range
else digitalWrite(a, LOW);
}
counter++;
if (counter > (maxCounter + (maxCounter/(highPin-lowPin))))
{
counter = 0;
}
//Serial.println(counter);
//Serial.print(counter);
//Serial.print(" ");
//Serial.println(maxCounter);
}
The code above is simplified by using a for loop between whatever int I set as the min and max range so I can easily set up every pint 3-13 by simply changing high pin to 13 and setting up the respective circuit.
//Switch LED circuit with Sin value
//sin wave
double xWave;
double yVal;
void setup() {
//Pin 4 and Pin 5 on
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
//xWave increase linear
xWave += 0.0005;
//yValue = sinX
yVal = sin(xWave);
//set 4 on when sinX is more than 0 else off
if (yVal > 0)
{
digitalWrite(4, HIGH);
} else
digitalWrite(4, LOW);
//set 5 on when sinX is less than 0 else off
if(yVal < 0)
{
digitalWrite(5, HIGH);
}else
digitalWrite(5, LOW);
//add sinX to voltage
analogWrite(3, 255 / 2 + (yVal * 255) / 2);
}
This Code creates a y value that is the sin of a proceeding X value. It uses 3 pins. 1 pin for each LED ground and it uses the Arduino's 3rd pin for adjusting voltage. The last line adds the sin of x to the output of pin 3 while the above conditions change turn pin 4 and 5 on or off when the sin is above or below 0. pin 4 and 5 are used for ground so by turning them on it actually polarizes them positive stopping the circuit meaning pin HIGH = LED off.
An obvious habit I need to improve is organizing the breadboard better. It's easy to just slap code together and cut out whatever I dont need but moving a circuit is much tentative so it's best that I make sure to stay consistent on construction.
For future projects with LEDs I'd want to try something with much more LEDs like a large matrix of LEDs which Im sure will be tricky to set up but the potential would be then migrating any expression in those LEDs into physical space, I could sew them into fabric, attach them to fabric, or make body wear out of them. I was recommended to to use LED arrays which sounds like a good asset since they have a small circuit for more precise data-flow than just on/off circuits. A stretch goal could be combining this with traditional media like a TV screen or music player only the space around the screen is manipulated with movement, mechanical noises and many LEDs that incorporates all dimensions of sensory.
Comments
Post a Comment