Skip to content

Instantly share code, notes, and snippets.

@DarioCasadevall
Created September 18, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarioCasadevall/58965da6dd89c4a07238f188cb59d9db to your computer and use it in GitHub Desktop.
Save DarioCasadevall/58965da6dd89c4a07238f188cb59d9db to your computer and use it in GitHub Desktop.
DJHS for the arduino
#include <Adafruit_NeoPixel.h>
#include "ps2dev.h" // to emulate a PS/2 device
#define KEYBOARD_PIN_DATA 2
#define KEYBOARD_PIN_CLOCK 3
#define motorPin1 37 //grau
#define motorPin2 39 //blau
#define motorPin3 41 //gruen
#define motorPin4 43 //lila
#define rotPinA 18
#define rotPinB 19
#define faderPin1 A0 //toControlSmall Led
//#define ctrlBig = A1; //LED gros noch nicht benutzt
#define mouseBut 21 //I-Button
#define pieper 10 //Arcade
#define controlPieper 52
#define pieper1 9
#define controlPieper1 50
#define pieper2 8
#define controlPieper2 48
#define play1 31 //An Aus Knopf
#define play2 32
#define ledSmall 13
#define ledBig 22
//Motor
unsigned int lowSpeed = 16000; // Notabene: nicht über 16000
unsigned int highSpeed = 1500;
//Keyboard
volatile PS2dev keyboard(KEYBOARD_PIN_CLOCK, KEYBOARD_PIN_DATA); // PS2dev object (2:data, 3:clock)
unsigned char c; //char stores data recieved from computer for KBD
int enabled = 1; // pseudo variable for state of "keyboard"
// Rotary Stuff
//int smoothingtemp = 0;
//volatile int outPut = 100;
//LEDs
const int ledSmallNumber = 8;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledSmallNumber, ledSmall, NEO_GRB + NEO_KHZ800);
const int ledBigNumber = 26;
Adafruit_NeoPixel stripBig = Adafruit_NeoPixel(ledBigNumber, ledBig, NEO_GRB + NEO_KHZ800);
int lightC = 0;
void setup()
{
// initialize the serial port:
Serial.begin(9600);
//BIGLED
strip.begin();
strip.show(); // Initialize all pixels to 'off'
stripBig.begin();
stripBig.show(); // Initialize all pixels to 'off'
//MotorPins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Setup Rotary Pins
pinMode(rotPinA, INPUT);
pinMode(rotPinB, INPUT);
pinMode(mouseBut, INPUT);
digitalWrite(mouseBut, HIGH);
//pinMode(play1, INPUT);
pinMode(pieper, OUTPUT);
pinMode(controlPieper, INPUT);
digitalWrite(controlPieper, HIGH);
//analogWrite(pieper, 1);
while(keyboard.write(0xAA)!=0);
delay(10);
//attach Interrupt Routines to Rotary Pins
attachInterrupt(digitalPinToInterrupt(rotPinA), rotaryUpdateA, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotPinB), rotaryUpdateB, CHANGE);
attachInterrupt(digitalPinToInterrupt(mouseBut), mouseDown, CHANGE);
//attachInterrupt(digitalPinToInterrupt(mouseBut), mouseUp, RISING);
colorWipe2(stripBig.Color(300,0,0), 500);
}
void loop()
{
//Serial.println(digitalRead(mouseBut));
linksrum(highSpeed);
//colorWipe(strip.Color(0,150,0), 500);
//colorWipe2(stripBig.Color(100,0,0), 500);
// int value1 = digitalRead(rotPinA);
// Serial.print(value1);
// int value2 = digitalRead(rotPinB);
// Serial.println(value2);
//
// Serial.print("OUTPUT: ");
// Serial.println(outPut);
int faderValue = digitalRead(play2);
Serial.println(faderValue);
// Serial.print(digitalRead(rotPinA));
// Serial.print(" ");
// Serial.println(digitalRead(rotPinB));
// int posA = digitalRead(rotPinA);
// int posB = digitalRead(rotPinB);
// delay(100);
// Serial.println(digitalRead(mouseBut));
// //scratch();
// pinMode(pieper, OUTPUT);
if(digitalRead(controlPieper) == HIGH) {
for (int i=50; i <= 150; i++){
analogWrite(pieper, i);
delay(1);
}
analogWrite(pieper, 0);
}
if(digitalRead(controlPieper1) == HIGH) {
analogWrite(pieper1, 100);
delay(100);
analogWrite(pieper1, 0);
}
if(digitalRead(controlPieper2) == HIGH) {
analogWrite(pieper2, 300);
delay(100);
analogWrite(pieper2, 0);
}
//Serial.println(digitalRead(controlPieper));
//resetBigLed();
lightC++;
if(lightC == 50) {
int y = map(analogRead(faderPin1), 960, 1025, 1, ledSmallNumber);
Serial.println(y);
for(int i=0;i<ledSmallNumber;i++){
if(i > y) {
strip.setPixelColor(i, strip.Color(0,0,0));
} else {
int randNumber = random(1, 5);
if(randNumber == 1) {
strip.setPixelColor(i, strip.Color(0,0,15));
}
if(randNumber == 2) {
strip.setPixelColor(i, strip.Color(0,15,15));
}
if(randNumber == 3) {
strip.setPixelColor(i, strip.Color(15,0,15));
}
if(randNumber == 4) {
strip.setPixelColor(i, strip.Color(15,15,15));
}
if(randNumber == 5) {
strip.setPixelColor(i, strip.Color(0,15,0));
}
// Moderately bright green color.
}
strip.show(); // This sends the updated pixel color to the hardware.
lightC = 0;
//rainbowCycle(0);
//delay(500); // Delay for a period of time (in milliseconds).
}
}}
void ack()
{
//acknowledge commands
while(keyboard.write(0xFA));
}
int keyboardcommand(int command)
{
unsigned char val;
switch (command)
{
case 0xFF: //reset
ack();
//the while loop lets us wait for the host to be ready
while(keyboard.write(0xAA)!=0);
break;
case 0xFE: //resend
ack();
break;
case 0xF6: //set defaults
//enter stream mode
ack();
break;
case 0xF5: //disable data reporting
//FM
enabled = 0;
ack();
break;
case 0xF4: //enable data reporting
//FM
enabled = 1;
ack();
break;
case 0xF3: //set typematic rate
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xF2: //get device id
ack();
keyboard.write(0xAB);
keyboard.write(0x83);
break;
case 0xF0: //set scan code set
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xEE: //echo
//ack();
keyboard.write(0xEE);
break;
case 0xED: //set/reset LEDs
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
}
}
void rotaryUpdateA() { //TODO: Debounce?
/* Wenn Pin A sich ändert (liegt fest, da rotaryUpdateA() aufgerufen wurde)
UND die beiden Pins dann gleich sind
-> liegt eine CW Rotation vor.
UND die beiden Pins dann ungleich sind
-> liegt eine CCW Rotation vor.
Siehe:
CW: 11 > 10 > [00] > 01 > [11]
CCW: 11 > [01] > 00 > [10] > 11
[AB] mögliche Zustände nach Veränderung von A
*/
int posA = digitalRead(rotPinA);
int posB = digitalRead(rotPinB);
// writek();
// writej();
// return;
if (posA == posB) {
//rotPos--;
writek();
//outPut--;
} else {
//rotPos++;
//outPut++;
writej();
}
}
void rotaryUpdateB() { //TODO: Debounce?
/* Wenn Pin B sich ändert (liegt fest, da rotaryUpdateB() aufgerufen wurde)
UND die beiden Pins dann gleich sind
-> liegt eine CCW Rotation vor.
UND die beiden Pins dann ungleich sind
-> liegt eine CW Rotation vor.
Siehe:
CW: 11 > [10] > 00 > [01] > 11
CCW: 11 > 01 > [00] > 10 > [11]
[AB] mögliche Zustände nach Veränderung von B
*/
int posA = digitalRead(rotPinA);
int posB = digitalRead(rotPinB);
if (posA == posB) {
//rotPos--;
writej();
} else {
//rotPos++;
writek();
}
}
void writej() {
//keyboard.write(0x1C); //(G HIGH)
keyboard.write(0x3B); //(G HIGH)
delay(20);
keyboard.write(0xF0); //(G RESET)
delay(20);
keyboard.write(0x3B); //(G RESET)
// keyboard.write(0xF0); //(G RESET)
//keyboard.write(0x1C); //(G RESET)
delay(700);
}
void writek() {
//keyboard.write(0x1C); //(G HIGH)
keyboard.write(0x42); //(G HIGH)
delay(20);
keyboard.write(0xF0); //(G RESET)
delay(20);
keyboard.write(0x42); //(G RESET)
//keyboard.write(0xF0); //(G RESET)
//keyboard.write(0x1C); //(G RESET)
delay(700);
}
void mouseUp() {
}
void mouseDown() {
//keyboard.write(0x1C); //(G HIGH)
keyboard.write(0x16); //(G HIGH)
delay(50);
keyboard.write(0xF0); //(G RESET)
delay(50);
keyboard.write(0x16); //(G RESET)
//keyboard.write(0xF0); //(G RESET)
//keyboard.write(0x1C); //(G RESET)
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void colorWipe2(uint32_t c, uint8_t wait) {
// for(uint16_t k=0; k<stripBig.numPixels(); k++) {
// stripBig.setPixelColor(k, c);
// stripBig.show();
// delay(wait);
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< stripBig.numPixels(); i++) {
stripBig.setPixelColor(i, Wheel(((i * 256 / stripBig.numPixels()) + j) & 255));
}
stripBig.show();
}
}
void rechtsrum(unsigned int motorSpeed)
{ // 1
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delayMicroseconds(motorSpeed);
// 2
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delayMicroseconds(motorSpeed);
// 3
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delayMicroseconds(motorSpeed);
// 4
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delayMicroseconds(motorSpeed);
// 5
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delayMicroseconds(motorSpeed);
// 6
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
delayMicroseconds(motorSpeed);
// 7
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delayMicroseconds(motorSpeed);
// 8
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delayMicroseconds(motorSpeed);
}
void linksrum(unsigned int motorSpeed)
{ // 1
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delayMicroseconds(motorSpeed);
// 2
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delayMicroseconds(motorSpeed);
// 3
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delayMicroseconds(motorSpeed);
// 4
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delayMicroseconds(motorSpeed);
// 5
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delayMicroseconds(motorSpeed);
// 6
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delayMicroseconds(motorSpeed);
// 7
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delayMicroseconds(motorSpeed);
// 8
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delayMicroseconds(motorSpeed);
}
void stop()
{ digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< stripBig.numPixels(); i++) {
stripBig.setPixelColor(i, Wheel(((i * 256 / stripBig.numPixels()) + j) & 255));
}
stripBig.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return stripBig.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return stripBig.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return stripBig.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment