top of page

Arduino Projects

Bluetooth

Workshop dust warning

Watering system

Illuminated bowl

Code for other projects

// LED light dance

//Allen Kaye March 7th 2017

/*
 * VCC on relay board to 5 v arduino
 * gnd to grd
 * IN1 to IN4 to Arduino 2, 3, 4 5
 * Dealy currently .5 second
 */

// Outputs a high to 4 relays in a random sequence
 
#define soundpin 8               
#define LED1 2                    //  4 Leds LED's on Digital output pins 3,4,5,6
#define LED2 3
#define LED3 4
#define LED4 5

int RANDOMVALUE = 0;
 
void setup()
{
 
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

           Serial.begin(9600);
            randomSeed(analogRead(0));
 
}
void loop()
{
   
     RANDOMVALUE = random(4);
      Serial.print("The Random Number is = ");
  Serial.println(RANDOMVALUE);
 
     
    if (RANDOMVALUE ==0) 
    {
       Serial.print("in 0");
             digitalWrite(LED1,LOW); 
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,HIGH);
      Serial.print("it is 0 ");
     } 
     else if (RANDOMVALUE ==1)  
     {
        Serial.print("in 1");
        digitalWrite(LED1,HIGH);  
             digitalWrite(LED2,LOW);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,HIGH);
         Serial.print("it is 1");
     }
    
 else if  (RANDOMVALUE ==2)     
      {
             digitalWrite(LED1,HIGH);
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,LOW); 
             digitalWrite(LED4,HIGH);
              Serial.print("it is 2");
        }
 
  else if (RANDOMVALUE ==3)     
      {
             digitalWrite(LED1,HIGH);
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,LOW);
         Serial.print("it is 3");
        }
        delay(400);
       
}

Other Code examples  for reference

/*
 Allen Kaye 2019

MSGEQ7 Demo app
 Look for the breakout board on www.whizoo.com
 
 This code runs on an Arduino Duemilanove, but will run on other Arduino models.
 
 Connections:
 - GND to GND on MSGEQ7 breakout board, and LED's
 - 5V to VDD on MSGEQ7 breakout board
 - A0 to OUT on MSGEQ7 breakout board
 - D7 to STROBE on MSGEQ7 breakout board
 - D8 to RESET on MSGEQ7 breakout board
 - D3 to LED 0 (indicator for frequency band 0)
 - D5 to LED 1 (indicator for frequency band 1)
 - D6 to LED 2 (indicator for frequency band 2)
 - D9 to LED 3 (indicator for frequency band 3)
 - D10 to LED 4 (indicator for frequency band 4)
 - D11 to LED 5 (indicator for frequency band 5)
 */
// Hardware-specific defines
#define MSGEQ7_STROBE_PIN      7
#define MSGEQ7_RESET_PIN       8
#define MSGEQ7_ANALOG_PIN      A0
#define NUM_FREQUENCY_BANDS    7
// Duemilanove only has 6 PWM outputs, so the last LED won't respond properly.  Your
// board may have more PWM outputs.  Typically you only want to monitor the lowest
// frequency bands because that is where the beat is.
int led[NUM_FREQUENCY_BANDS] = {3, 5, 6, 9, 10, 11, 0};

// There is a concept of "persistence of vision" with LED's.  The LED has to be on long enough
// for the eye to recognise that it is on.  When a high volume is received on a frequency band,
// The LED is turned on (at a high PWM value) and then gradually faded until the next beat in
// that frequency.
int ledPWMValue[NUM_FREQUENCY_BANDS] = {0, 0, 0, 0, 0, 0, 0};
  void setup() {
  // Set the LED pins as outputs
  for (int i=0; i<NUM_FREQUENCY_BANDS; i++)
    pinMode(led[i], OUTPUT);
    // Set up the MSGEQ7 IC
  pinMode(MSGEQ7_ANALOG_PIN, INPUT);
  pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
  pinMode(MSGEQ7_RESET_PIN, OUTPUT);
  digitalWrite(MSGEQ7_RESET_PIN, LOW);
  digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
}
// This loop executes around 100 times per second
void loop() {
  int frequencyBandVolume;
    // Toggle the RESET pin of the MSGEQ7 to start reading from the lowest frequency band
  digitalWrite(MSGEQ7_RESET_PIN, HIGH);
  digitalWrite(MSGEQ7_RESET_PIN, LOW);
 
  // Read the volume in every frequency band from the MSGEQ7
  for (int i=0; i<NUM_FREQUENCY_BANDS; i++) {
    digitalWrite(MSGEQ7_STROBE_PIN, LOW);
    delayMicroseconds(30); // Allow the output to settle
    frequencyBandVolume = analogRead(MSGEQ7_ANALOG_PIN);
    digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
   
    // The read value is 10-bit (0 to 1024).  PWM needs a value from 0 to 255, so divide by 4
    frequencyBandVolume = frequencyBandVolume >> 2;
   
    // Fade the current LED value for this band
    ledPWMValue[i] = ledPWMValue[i] > 7? ledPWMValue[i] - 7 : 0;
   
    // Don't show the lower values
    if (frequencyBandVolume > 70) {
      // If the new volume is greater than that currently being showed then show the higher volume
      if (frequencyBandVolume > ledPWMValue[i])
        ledPWMValue[i] = frequencyBandVolume;
    }
        // Set the LED PWM value to the frequency band's volume
    analogWrite(led[i],  ledPWMValue[i]);
  }
   // Wait before executing this loop again
  delay(10);
}
 

/*
 Allen K

 

Original 2018 Updated Feb 2020

Moisture controller using Analog Input. One moisture sensor connected to A0. One relay and water pump triggered ON when PIN 7 is HIGH 
 */
#include <SPI.h>
#include <SD.h>

//const int chipSelect = 4;

int sensorPin1 = A0;    // select the input pin for the potentiometer
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorPin2 = A1;
int sensorValue2 = 0;


int relay1=4;
int relay5=5;
int relay6=6;
int relay7=7;
int led5=5;
int led6=6;
int led8=8;
int loops = 0;

int i = 0;
int j = 0;

File dataFile;

void setup()


{

Serial.begin(9600);

/* while(!Serial)
    {
      ; // wait for serial port to connect
      Serial.println("waiting for serial port to connect");
    }

if (!SD.begin(chipSelect))
    {
      Serial.println("Card failed or not in holder");
      return;
    }
*/
  pinMode(relay7, OUTPUT);
  Serial.println("Starting");

pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led8, OUTPUT);

digitalWrite(led5,HIGH);
digitalWrite(led6,HIGH);
digitalWrite(led8,HIGH);

delay(2000);

digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led8,LOW);
 
}

void loop() {

digitalWrite(led6,HIGH);
delay(1000);
digitalWrite(led6,LOW);
  // read the value from the sensor at A0:

  sensorValue1 = analogRead(sensorPin1);
  sensorValue2 = analogRead(sensorPin2);
 
  Serial.println(sensorValue1);

/*  String dataString = "";

  dataString += String(sensorValue);
  dataString += ",";

dataFile = SD.open("ghouse1.txt", FILE_WRITE);
Serial.println("testgreenhouse should be open");
 
 if (dataFile)   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println("written an item of data");

    */

    for (i=0;i<4;i++) {
    digitalWrite(led5, HIGH);
    delay(500);
    digitalWrite(led5, LOW);
    delay(500);
    Serial.println("Led 5 flash");
    }
/*   
 // }
*/
   digitalWrite(relay7,LOW);

if (sensorValue1>800) {
  Serial.println("Too DRY");
     for (i=0;i<6;i++){
      digitalWrite(led8, HIGH);
    delay(500);
    digitalWrite(led8, LOW);
    delay(500);
     }

     digitalWrite(relay7,HIGH);
   delay(10000);
   digitalWrite(relay7,LOW);


}
else {

   Serial.println("wet enough for now");
   digitalWrite(relay7,LOW);
}

  digitalWrite(relay7,LOW);

/* if (sensorValue2>800) {
  Serial.println("Too DRY");
     for (i=0;i<6;i++){
      digitalWrite(led8, HIGH);
    delay(500);
    digitalWrite(led8, LOW);
    delay(500);
     }
    digitalWrite(relay6,HIGH);
    delay(10000);
    digitalWrite(relay6,LOW);
   
  // dataFile.println("On");
  //  dataFile.close();
}
else {

   Serial.println("Second sensor wet enough for now");
   digitalWrite(relay6,LOW);
}
delay(2000);
   digitalWrite(led6,HIGH);
    delay(2000);
    digitalWrite(led6,LOW);
Serial.println("cycle ends");
*/
   Serial.println("loop delay now");
  delay(20000);
   Serial.println("loop delay end");
}
 

//

Allen Kaye demon 2019

LED light dance
 
#define soundpin 8               //  reads the power from the light sensor from Analog input 0
#define LED1 3                    //  4 Leds LED's on Digital output pins 3,4,5,6
#define LED2 4
#define LED3 5
#define LED4 6
 
int sound;
 
void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
 
  // Provide power by using the analog inputs as normal digital pins. 
  pinMode(soundpin, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  digitalWrite(LED1,HIGH);   // set the LEDs on
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,HIGH);
             delay(1000);
             digitalWrite(LED1,LOW);   // set the LEDs on
             digitalWrite(LED2,LOW);
             digitalWrite(LED3,LOW);
             digitalWrite(LED4,LOW);
 
}
void loop()
{
      sound=analogRead(soundpin);   // this samples the sound constantly
      Serial.println(sound);
     
      if((sound)>30)
        {
             digitalWrite(LED1,HIGH);   // set the LEDs on
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,HIGH);
        } 
        else if((sound)>20)
        {
             digitalWrite(LED1,HIGH);   // set the LED on
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,HIGH);
             digitalWrite(LED4,LOW);   // set the LED off
        }
        else if(sound>10)
        {
             digitalWrite(LED1,HIGH);   // set the LED on
             digitalWrite(LED2,HIGH);
             digitalWrite(LED3,LOW);   // set the LED off
             digitalWrite(LED4,LOW);
        }
         else if(sound>5)
        {
             digitalWrite(LED1,HIGH);   // set the LED on
             digitalWrite(LED2,LOW);   // set the LED off
             digitalWrite(LED3,LOW);
             digitalWrite(LED4,LOW);
        }
         else
        {
             digitalWrite(LED1,LOW);   // set the LEDs off
             digitalWrite(LED2,LOW);
             digitalWrite(LED3,LOW);
             digitalWrite(LED4,LOW);
        }
      
     Serial.println(soundpin);              //output for serial monitor
 
     delay(5);                                   // And a shot delay
}

bottom of page