#include "Stepper.h"
#include "Keypad.h"
#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#include "FastLED.h"
/*----- Variables, Pins -----*/
#define DATA_PIN A4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 120
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 250
#define FRAMES_PER_SECOND 120
#endif
#define PIN A6 //power
#define PIN A7 //reset
#define PIN A4 //RGBs
#define STEPS 32 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
int receiver = A5; // Signal Pin of IR receiver to Arduino Digital Pin 12
int trigPin = 10;
int echoPin = 11;
int buzzer = 12;
int power = A6;
int reset = A7;
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, A0, A2, A1, A3);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, A4, NEO_GRB + NEO_KHZ800);
//Deklarationen Keypad
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Hier die richtigen Pins eintragen!!! Anschlussgrafik gibt es z.B. hier http://www.learningaboutelectronics.com/Articles/Arduino-keypad-circuit.php
byte rowPins[numRows] = {9, 8, 7, 6};
byte colPins[numCols] = {5, 4, 3, 2};
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
irrecv.enableIRIn(); // Start the receiver
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(power, OUTPUT);
pinMode(reset, OUTPUT);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { sinelon,};
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns ,0white,250red,100green,150blue
void loop()
{
char keypressed = myKeypad.getKey(); //Tastedruck am Keypad erfassen
if (keypressed == 'A')
motorLaufen(8700);
if (keypressed == 'B')
motorLaufen(-8700);
if (keypressed == '1')
colorWipe(strip.Color(127, 127, 127), 30); // White
if (keypressed == '*')
digitalWrite(power, HIGH );
digitalWrite(power, LOW );
if (keypressed == '#')
digitalWrite(reset, HIGH );
digitalWrite(reset, LOW );
if (irrecv.decode(&results)) {
if (results.value == 16761405) // forward pressed
motorLaufen(8700);
if (results.value == 16712445) // backwards pressed
motorLaufen(-8700);
if (results.value == 16738455) // taste 0
colorWipe(strip.Color(127, 127, 127), 30); // White
if (results.value == 16724175) // taste 1
colorWipe(strip.Color(0, 255, 0), 30); // Green
if (results.value == 16718055)//taste 2
theaterChase(strip.Color(127, 127, 127), 1000); // White
if (results.value == 16743045) //taste 3
colorWipe(strip.Color( 255, 100, 0), 30); // Orange
if (results.value == 16716015)//taste 4
colorWipe(strip.Color( 255, 0, 0), 30); // red
if (results.value == 16726215)//taste 5
theaterChase(strip.Color(0, 255, 0), 500); // Green
if (results.value == 16734885)//taste 6
theaterChase(strip.Color(0, 0, 255), 1000); // Blue
if (results.value == 16728765)//taste 7
colorWipe(strip.Color(0, 0, 255), 30); // blue
if (results.value == 16730805)//taste 8
theaterChase(strip.Color(255, 0, 0), 100); // Red
if (results.value == 16732845)//taste 9
theaterChase(strip.Color(255, 100, 0), 1000); // Orange
if (results.value == 16769055)//taste EQ
rainbow(20);
if (results.value == 16748655)//taste vol +
rainbowCycle(10);
if (results.value == 16754775)//taste vol -
theaterChaseRainbow(50);
if (results.value == 16753245)//taste Power
// Call the current pattern function once, updating the 'leds' array
{ gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (results.value == 16736925)//taste Mode
rainbow(20);
if (results.value == 16769565)//taste Mute
rainbow(20);
if (results.value == 16720605)//taste Pause
rainbow(20);
if (results.value == 16750695)//taste gehe zurück
rainbow(20);
if (results.value == 16756815)//taste U/SD
rainbow(20);
}
if (millis() % 10 == 0)
{ long duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 40) {
digitalWrite(buzzer, HIGH );
theaterChase1(strip.Color(255, 0, 0), 100); // Red
strip.show();
}
else {
digitalWrite(buzzer, LOW );
}
if (distance > 200) {
Serial.println("Out of range");
}
if (distance < 0) {
Serial.println("Out of Range");
}
}
}
/* --end main loop -- */
void motorLaufen(int mSteps) {
theaterChase(strip.Color(255, 100, 0), 1000); // Orange
small_stepper.setSpeed(1000);
Steps2Take = mSteps; // Rotate CCW
small_stepper.step(Steps2Take);
irrecv.resume(); // receive the next value
strip.show();
MotorAus();
}
void MotorAus ()
{
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
}
// 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);
}
irrecv.resume();
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
for (int q = 0; q < 2; q++) {
for (uint16_t i = 0; i < strip.numPixels(); i = i + 2) {
strip.setPixelColor(i + q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i = 0; i < strip.numPixels(); i = i + 2) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
irrecv.resume();
}
//Theatre-style crawling lights.
void theaterChase1(uint32_t c, uint8_t wait) {
for (int j = 0; j < 2; j++) { //do 10 cycles of chasing
for (int q = 0; q < 2; q++) {
for (uint16_t i = 0; i < strip.numPixels(); i = i + 2) {
strip.setPixelColor(i + q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i = 0; i < strip.numPixels(); i = i + 2) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
irrecv.resume();
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
irrecv.resume();
}
// Slightly different, this makes the rainbow equally distributed throughout
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 < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
irrecv.resume();
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
irrecv.resume();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(8, 0, NUM_LEDS); //speed,start-stop,
leds[pos] += CHSV( gHue, 0, 192); // gHue,Multicolor 0=whit 255=rainbow fade ,Bightness
}