Bluetooth speaker with leds

Jag har tänkt göra två gammal stereo högtalare till en bluetooth högtalare med addressable led som lyser/blinkar.

dav

Arduino koden

const int ledPin =  13;         
int ledState = LOW;             

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 100;           // interval at which to blink (milliseconds)

int program = 1;

void setup() 
  
  {  
  pinMode(ledPin, OUTPUT);
  pinMode(2, INPUT); 
  pinMode(4, INPUT);
  Serial.begin(9600); 
  }

void loop() 
  
  {
  int buttonState = digitalRead(2);
  int buttonState2 = digitalRead(4);

    if(buttonState == HIGH)
    {   
     program = 1;
    }
  
   if (buttonState2 == HIGH) 
    {
     program = 2;
    }

  if(program == 1)
    {
    alwaysOn();
    }

  if(program == 2)
    {
    blinking();
    }
}

void alwaysOn()   // program 1
{
  digitalWrite(ledPin, HIGH); 
}

void blinking()   // program 2
{
  unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval) 
    {
      previousMillis = currentMillis;
        if (ledState == LOW) 
          {
          ledState = HIGH;
          } 
        else 
          {
          ledState = LOW;
          }
    } 
   digitalWrite(ledPin, ledState);
}

—————-

const int ledPin =  13;         
int ledState = LOW;             

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 100;           // interval at which to blink (milliseconds)
const long interval2 = 500;

int program = 1;

void setup() 
  
  {  
  pinMode(ledPin, OUTPUT);
  pinMode(2, INPUT); 
  pinMode(4, INPUT);
  Serial.begin(9600); 
  }

void loop() 
  
  {
  int buttonState = digitalRead(2);
  int buttonState2 = digitalRead(4);
  int buttonState3 = digitalRead(6);
  int buttonState4 = digitalRead(8);

    if(buttonState == HIGH)
    {   
     program = 1;
    }
  
   if (buttonState2 == HIGH) 
    {
     program = 2;
    }

   if (buttonState3 == HIGH)
    {
      program = 3;
    }

   if (buttonState4 == HIGH)
    {
      program = 4;
    }

  if(program == 1)
    {
    alwaysOn();
    }

  if(program == 2)
    {
    blinking();
    }

   if(program == 3)
    {
      blinkingSlow();
    }

   if(program == 4)
    {
      blinkingRandom();
    }
}

void alwaysOn()   // program 1
{
  digitalWrite(ledPin, HIGH); 
}

void blinking()   // program 2
{
  unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval) 
    {
      previousMillis = currentMillis;
        if (ledState == LOW) 
          {
          ledState = HIGH;
          } 
        else 
          {
          ledState = LOW;
          }
    } 
   digitalWrite(ledPin, ledState);
}

void blinkingSlow()
{
    unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval2) 
    {
      previousMillis = currentMillis;
        if (ledState == LOW) 
          {
          ledState = HIGH;
          } 
        else 
          {
          ledState = LOW;
          }
    } 
   digitalWrite(ledPin, ledState);
}

void blinkingRandom()
{
      unsigned long currentMillis = millis();
      
      int randomnumber = random(50,300);
    
    if (currentMillis - previousMillis == randomnumber) 
    {
      previousMillis = currentMillis;
        if (ledState == LOW) 
          {
          ledState = HIGH;
          } 
        else 
          {
          ledState = LOW;
          }
    } 
   digitalWrite(ledPin, ledState);
}


#include "FastLED.h"

#define NUM_LEDS 90


#define DATA_PIN 10
//#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];


const int ledPin =  10;         
int ledState = LOW;             

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 100;           // interval at which to blink (milliseconds)
const long interval2 = 500;

int program = 1;


void setup() { 
  
     LEDS.setBrightness(30);
      FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

  pinMode(ledPin, OUTPUT);
  pinMode(2, INPUT); 
  pinMode(4, INPUT);
  Serial.begin(9600); 
}

void loop() { 

  int buttonState = digitalRead(2);
  int buttonState2 = digitalRead(4);


      if(buttonState == HIGH)
    {   
     program = 1;
    }
  
   if (buttonState2 == HIGH) 
    {
     program = 2;
    }

  if(program == 1)
    {
    Random();
    }

  if(program == 2)
    {
    Blue();
    }

    
  leds[0] = CRGB::Red;
  FastLED.show();
  delay(500);

  leds[0] = CRGB::Black;
  FastLED.show();
  delay(500);
}

void Random()
{
  int randomnumber1 = random(0,255);
  int randomnumber2 = random(0,255); 
  int randomnumber3 = random(0,255);
  int randomnumber4 = random(0,255);
  int randomnumber5 = random(0,255); 
  int randomnumber6 = random(0,255);
  
  for(int i = 0; i <=90; i+=1)
  {
  leds[i] = CRGB(randomnumber1,randomnumber2,randomnumber3);
  FastLED.show();
  delay(5);
  }
 for(int i = 90; i >=0; i-=1)
  {
  leds[i] = CRGB(randomnumber4,randomnumber5,randomnumber6);
  FastLED.show();
  delay(5);
  }
}

void Blue()
{
    for(int i = 0; i <=90; i+=1)
  {
  leds[i] = CRGB::Blue;
  FastLED.show();
  delay(5);
  }
 for(int i = 90; i >=0; i-=1)
  {
  leds[i] = CRGB::Red;
  FastLED.show();
  delay(5);
  }
}

void blinkingRandom()
{
      unsigned long currentMillis = millis();
      
      int randomnumber = random(50,300);
    
    if (currentMillis - previousMillis == randomnumber) 
    {
      previousMillis = currentMillis;
        if (ledState == LOW) 
          {
          ledState = HIGH;
          } 
        else 
          {
          ledState = LOW;
          }
    } 
   digitalWrite(ledPin, ledState);
}

Jag hann inte riktigt färdigt med projektet. Jag har fått bluetooth förstärkaren och led:arna men jag har inte hunnit koppla ihop allting. Koden är så gott som färdig. Nu slutar jag med mitt projekt.

dav

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s