Skip to content

Instantly share code, notes, and snippets.

@smartameer
Created December 25, 2018 11:35
Show Gist options
  • Save smartameer/5353b6107cea333f7b98e64dad4d3954 to your computer and use it in GitHub Desktop.
Save smartameer/5353b6107cea333f7b98e64dad4d3954 to your computer and use it in GitHub Desktop.
Pulse width modulation analog output example by adding led to arduino nano
int i = 0;
int j = 0;
const int W = 6; // white, 2 white connected to same pin
const int R2 = 3; // red
const int R1 = 11; // red
const int G1 = 10; // green
const int G2 = 5; // green
const int B = 9; // blue
void setup() {
pinMode(W, OUTPUT);
pinMode(B, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(G1, OUTPUT);
pinMode(G2, OUTPUT);
}
void loop() {
int k = 5;
for( j = 0; j < k; j++) {sequence();}
delay(1000);
for( j = 0; j < k; j++) {fadeIn();}
delay(1000);
for( j = 0; j < k; j++) {fadeInColor();}
delay(1000);
for( j = 0; j < k; j++) {rotate();}
delay(1000);
for( j = 0; j < k; j++) {oppSequence();}
delay(1000);
}
void oppSequence() {
for (int i = 0; i < 255; i++) {analogWrite(B, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(G1, i);analogWrite(G2, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(R1, i);analogWrite(R2, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(W, i);delay(1);}
}
}
}
for (int i = 255; i >= 0; i--) {analogWrite(B, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(G1, i);analogWrite(G2, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(R1, i);analogWrite(R2, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(W, i);delay(1);}
}
}
}
}
void rotate() {
digitalWrite(W, HIGH);
for (int i = 0; i < 255; i++) {analogWrite(R2, 254-i);analogWrite(R1, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(R1, 254-i);analogWrite(G1, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(G1, 254-i);analogWrite(B, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(B, 254-i);analogWrite(G2, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(G2, 254-i);analogWrite(R2, i);delay(1);}
}
}
}
}
}
void fadeInColor() {
for (int i = 0; i < 255; i++) { analogWrite(W, i);delay(1); }
{
for (int i = 0; i < 255; i++) {analogWrite(R1, i);analogWrite(R2, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(G1, i);analogWrite(G2, i);delay(1);}
{
for (int i = 0; i < 255; i++) {analogWrite(B, i);delay(1);}
}
}
}
for (int i = 255; i >= 0; i--) {analogWrite(B, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(G1, i);analogWrite(G2, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(R1, i);analogWrite(R2, i);delay(1);}
{
for (int i = 255; i >= 0; i--) {analogWrite(W, i);delay(1);}
}
}
}
}
void fadeIn() {
int pins[6] = { W, R1, G1, B, G2, R2 };
for (int j = 0; j < 6; j++) {
for (int i = 0; i < 255; i++) {analogWrite(pins[j], i);delay(1);}
for (int i = 255; i >= 0; i--) {analogWrite(pins[j], i);delay(1);}
}
}
void sequence() {
int pins[7] = { W, R1, G1, B, G2, R2, W };
for (int i = 0; i < 255; i++) {
for (int j = 0; j < 7; j++) {
analogWrite(pins[j], i);
}
delay(3);
}
for (int i = 255; i >= 0; i--) {
for (int j = 0; j < 7; j++) {
analogWrite(pins[j], i);
}
delay(3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment