8.7 Arduino: Processing Controls

 

Processing Code:

import controlP5.*;
import processing.serial.*;

Serial port1;

ControlP5 cp5;

float LED_num;


void setup() {
  size(800,800);
  
  String portName = Serial.list()[1];
  port1 = new Serial(this, portName, 115200); 
  
  cp5 = new ControlP5(this);

   cp5.addSlider("LED_num")
     .setPosition(50,50)
     .setSize(200,20)
     .setRange(0,20)
     .setValue(3)
     .setColorForeground(color(20,200,200))
     .setColorLabel(color(255))
     .setColorBackground(color(70,70,70))
     .setColorValue(color(0,0,0))
     .setColorActive(color(0,255,255))
     ;

}

void draw(){
  background(0);

  port1.write(int(LED_num));

}

Ardunio Code:

#include 
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 21

int num;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)


// setup() function -- runs once at startup --------------------------------

void setup() {

  Serial.begin(115200);

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)
}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {

 if(Serial.available() ){
   num = Serial.read();
 }

  for(int i=0; i<strip.numPixels(); i++) {
    if(i< num){ 
      strip.setPixelColor(i, strip.Color(255,255,255)); 
    }else{
      strip.setPixelColor(i,strip.Color(0,0, 0)); 
    }

    strip.show();                          //  Update strip to match    
  }
  Serial.print(78);
}