Sunday 16 December 2018

Arduino Xmas Lights with songs


Video: 



Parts: (all parts can be bought from Jaycar or ebay/amazon etc.)

3x  IRLZ34N Mosfet's
3x 10K Resistors
1x Arduino Uno/Nano etc
12v Power Supply
1x Roll RGB Led strip, 5M.
1x Piezo speaker


Wiring Diagram


Code:


//led defines and variables
#define RED_PIN 6
#define GREEN_PIN 9
#define BLUE_PIN 5
int RM = 1, GM = 1, BM = 1;
int R = 0, G = 0, B = 0;
int ColountCount = 0;


//music defines and veriables
#define SpeakerPin 12 //change to suit the speaker out wiring
int duration = 0;
char notes[] = "AA";
int beats[] = {1, 1};
int tempo = 165;
char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 958, 852, 758 };
void setup()
{
  pinMode(SpeakerPin, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
  Song4(); Song5();
  Song6(); Song7(); Song8(); Song9();
  Song10();
}

void Song4() {
  int duration = 69;
  char notes[] = "ddaagfedcdefga ddaagfedcdefga avgavCDagfdefgfgavaagfedfedgfgavCDagfed";
  int beats[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 1, 1, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}
void Song5() {
  int duration = 71;
  char notes[] = "cfffgagavCavafggfcfffgagavCavafggffaCDCvagfgavCcfagfccfffgagavCavafggf ";
  int beats[] = { 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song6() {
  int duration = 29;
  char notes[] = "faagfvvagaCCbCaDCvagfeagffef ";
  int beats[] = { 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song7() {
  int duration = 51;
  char notes[] = "aavCagfgagdgavCaggfgagff vavCDDaaCagfecavCagfgagff ";
  int beats[] = { 2, 3, 1, 2, 2, 4, 4, 3, 1, 2, 2, 8, 3, 1, 2, 2, 3, 1, 4, 2, 2, 3, 1, 6, 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 4, 4, 2, 2, 3, 1, 8, 8};
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song8() {
  int duration = 77;
  char notes[] = "cffefaagCCCvagacffefaagCffeedcCCCfvaagCCCfvaagDDDCvavgavCffgaDDDCvavgavCffgf ";
  int beats[] = {2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 3, 1, 2, 2, 4, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 3, 1, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 1, 1, 3, 1, 2, 2, 4, 3, 1, 2, 2, 2, 2, 4, 2, 1, 1, 3, 1, 2, 2, 4, 8};
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song9() {
  int duration = 64;
  char notes[] = "ggdgadbabCbaggsesgabsedd DCbCbabgasedggsgagdbbabCbabCbagsgCbagg ";
  int beats[] = { 2, 4, 2, 2, 4, 4, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 3, 1, 6, 2, 4, 2, 2, 4, 4, 2, 2, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 3, 1, 6, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}
void Song10() {
  int duration = 63;
  char notes[] = "egbbbaCbagabgegasedeaaeesgsedgabbbaCbag DD bb baCdagabgegasede ";
  int beats[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 4, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

int colourcount = 1;
void PulseLed() {
  colourcount++;
  if (colourcount > 3)colourcount = 1;

  if (colourcount == 1) {
    analogWrite(RED_PIN, 255);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 0);
  }
  if (colourcount == 2) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 255);
    analogWrite(BLUE_PIN, 0);
  }
  if (colourcount == 3) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 255);
  }


}

void playNote(char note, int duration) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(SpeakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(SpeakerPin, LOW);
    delayMicroseconds(tone);
  }
}

Friday 7 December 2018

Slice2Video for ChituBox and Shuffle

Download.  (Shuffle Version)
Download. (Selectable X and Y size version)

Usage: Extract the Chitubox zip to a folder, select your compression ratio (lower = less quality, higher = bigger filesize), and FPS (higher FPS = faster playback), click GO, navigate into your extracted folder and give the video file a name. Software will loop through each frame in the preview window, when its done, its done.

Compiled against .net4.5 so old win7 users (why the hell you still on win7?!?!) can use it.

Output Sample Video:

Usage Video:

Code: (compile with reference to sharpavi)
using SharpAvi.Codecs;
using SharpAvi.Output;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Slice2video
{
    public partial class ShuffleSlice2Video : Form
    {
        int X = 2560,
            Y = 1440,
            framerate = 15,
            Quality = 70;

        private void FrameRateBar_Scroll(object sender, EventArgs e)
        {
            framerate = (int)FrameRateBar.Value;
            Flabel.Text = "FrameRate " + framerate + "FPS";
        }

        private void QualityBar_Scroll(object sender, EventArgs e)
        {
            Quality = (int)QualityBar.Value;
            Qlabel.Text = "Quality " + Quality + "%";
        }

        public ShuffleSlice2Video()
        {InitializeComponent();}

        private void button1_Click_1(object sender, EventArgs e)
        {
            string file_name, srcdirectory;         
            var size = new Size(X, Y);
            var file_dialog = new SaveFileDialog()
            {
                Title = "Pick folder with the PNG's, name the output.",
                Filter = "Video Files (*.avi)|*.avi",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            };

            if (file_dialog.ShowDialog() == DialogResult.OK)
            {
                file_name = file_dialog.FileName;
                srcdirectory = Path.GetDirectoryName(file_name);
                var FileList = Directory.GetFiles(srcdirectory, "*.png", SearchOption.TopDirectoryOnly).ToList();
                var writer = new AviWriter(file_name){FramesPerSecond = framerate,EmitIndex1 = true};
                var encoder = new MotionJpegVideoEncoderWpf(X, Y, Quality);
                var stream = writer.AddEncodingVideoStream(encoder, width: X, height: Y);
                for(int i= 1;i<FileList.Count-2;i++)
                {
                    Bitmap img_save = new Bitmap(srcdirectory+@"\\"+i+".png");
                    img_save.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    pictureBox1.Image = img_save;
                    Application.DoEvents();
                    var buffer = new byte[X * Y * 4];
                    var bits = img_save.LockBits(new Rectangle(0, 0, X, Y), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                    Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
                    img_save.UnlockBits(bits);
                    stream.WriteFrame(true, buffer, 0, buffer.Length);
                    bits = null;
                    img_save.Dispose();
                } 
                writer.Close();
            };
        }
    }
}