Computer / Handy Tutorial <br> Arduino Projekte


Arduino Code


#include <FastLED.h>

#define LED_PIN     3
#define NUM_LEDS    25
#define BRIGHTNESS  250
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100


CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

int ledstate;

uint8_t gHue = 0; // rotating "base color" used by many of the patterns

void setup() {
    delay( 3000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );

    Serial.begin(9600);
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
}


void loop(){
  loopstart:
  
    if(Serial.available() > 0){           //if some data is available of in the serial port
      char serialinput = Serial.read();   //read the value
    
      if(serialinput == '1'){             //if statement will be true(1)
        ledstate = 1;   
      }
      if(serialinput == '0'){             //if statement will be false(0)
        ledstate = 0;
      }
      if(serialinput == '2'){             //if statement will be true(1)
        ledstate = 2;   
      }
      if(serialinput == '3'){             //if statement will be false(0)
        ledstate = 3;
      }
      if(serialinput == '4'){             //if statement will be true(1)
        ledstate = 4;   
      }
      if(serialinput == '5'){             //if statement will be false(0)
        ledstate = 5;
      }
      if(serialinput == '6'){             //if statement will be true(1)
        ledstate = 6;   
      }
      if(serialinput == '7'){             //if statement will be false(0)
        ledstate = 7;
      }
    }
    
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; 

    if(ledstate == 1){
    FillLEDsFromPaletteColors( startIndex);
    }
    if(ledstate == 3){
    juggle();
    }
    if(ledstate == 5){
    confetti();
    }
    if(ledstate == 7){
    sinelon();
    }
    if((ledstate == 0) || (ledstate == 2) || (ledstate == 4) || (ledstate == 6)){
      LEDS.clear();
      }
    FastLED.show();
    FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex){
    uint8_t brightness = 255;
    
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;
    }
}

void juggle() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte dothue = 0;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
    dothue += 32;
  }
}

void confetti(){
  // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUM_LEDS, 10);
  int pos = random16(NUM_LEDS);
  leds[pos] += CHSV( gHue + random8(64), 200, 255);
}

void sinelon(){
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, NUM_LEDS, 20);
  int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  leds[pos] += CHSV( gHue, 255, 192);
}


Processing Code


// Processing application that demonstrates the Button class by creating a button
// Draws a square in the window when the mouse cursor is over the button
// Writes to the Processing IDE console pane when the button is clicked
// 3 July 2015    http://startingelectronics.org
Button on_button;  // the button
Button a_button;
Button b_button;
Button c_button;

import processing.serial.*;      //import the library for a serial communication (sketch-import library-serial)
int send;
int asend;
int bsend;
int csend;

Serial myPort; 

PFont font1;

 

void setup() {
  myPort=new Serial(this, "COM30", 9600);  //se the name of our communication port (Arduino COM port)
  
  size (300, 200);
  smooth();
  
  font1 = loadFont("ArialMT-48.vlw");
  
  // create the button object
  on_button = new Button("Rainbow", 20, 40, 100, 50);
  a_button = new Button("Junge", 180, 40, 100, 50);
  b_button = new Button("Confetti", 20, 120, 100, 50);
  c_button = new Button("Sinelon", 180, 120, 100, 50);
  
}

void draw() {
  background(24,116,205);
  textFont(font1, 22);
  
  text("PC-Cooler", 150, 20);
  
  // draw a square if the mouse curser is over the button
  if (on_button.MouseIsOver()) {

  }
  else {
    // hide the square if the mouse cursor is not over the button
  }
  // draw the button in the window
  textFont(font1, 18);
  on_button.Draw();
  a_button.Draw();
  b_button.Draw();
  c_button.Draw();
  

}

// mouse button clicked
void mousePressed(){
  if (on_button.MouseIsOver()) {
    print("Clicked: ");
    if (send == 0){
      myPort.write('1');
      send = 1;
      println("1");
    }
    else if (send == 1){
      myPort.write('0'); 
      send = 0;
      println("0");
    }
  }
  
  if (a_button.MouseIsOver()) {
    print("Clicked: ");
    if (asend == 0){
      myPort.write('3');
      asend = 1;
      println("3");
    }
    else if (asend == 1){
      myPort.write('2'); 
      asend = 0;
      println("2");
    }
  }
  
  if (b_button.MouseIsOver()) {
    print("Clicked: ");
    if (bsend == 0){
      myPort.write('5');
      bsend = 1;
      println("5");
    }
    else if (bsend == 1){
      myPort.write('4'); 
      bsend = 0;
      println("4");
    }
  }
  
  if (c_button.MouseIsOver()) {
    print("Clicked: ");
    if (csend == 0){
      myPort.write('7');
      csend = 1;
      println("7");
    }
    else if (csend == 1){
      myPort.write('6'); 
      csend = 0;
      println("6");
    }
  }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// the Button class
class Button {
  String label; // button label
  float x;      // top left corner x position
  float y;      // top left corner y position
  float w;      // width of button
  float h;      // height of button
  
  // constructor
  Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
  
  boolean MouseIsOver() {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
      return true;
    }
    return false;
  }
}

class stay{
  void time(){
    myPort.write(99);
    println("send99");
    delay(1000);
  }
}