light a LED, brightness control?
in
Integration and Hardware
•
2 years ago
hello, thank you very much for from now if it helps.
is that I can turn on and control the brightness of the LED, but separately.
the problem is you can not make a whole arduino differentiate me from a character, that is sending an "A" on, and an integer 65 to the brightness of the LED. Arduino LEE ME, ME INTERPRET THESE ARE THE SAME.
ASCII CODE THAT IS THESE ARE THE SAME, BUT HOW CAN I DO TO ME THAT ARDUINO Diff ??.
code -processing
is that I can turn on and control the brightness of the LED, but separately.
the problem is you can not make a whole arduino differentiate me from a character, that is sending an "A" on, and an integer 65 to the brightness of the LED. Arduino LEE ME, ME INTERPRET THESE ARE THE SAME.
ASCII CODE THAT IS THESE ARE THE SAME, BUT HOW CAN I DO TO ME THAT ARDUINO Diff ??.
code -processing
- import processing.serial.*; // serial library
Serial[] myPorts = new Serial[1]; // lets only use one port in this sketch
// GUI variables
import controlP5.*; // controlP5 library version of controlP5 (0.5.7)
ControlP5 controlP5; // create the handler to allow for controlP5 items
Textlabel txtlblWhichcom; // text label displaying which comm port is being used
Textarea commTextarea; // text area displaying what has been received on the serial port
ListBox commListbox; // list of available comm ports
// setup
void setup() {
size(600,400);
frameRate(30);
controlP5 = new ControlP5(this); // initialize the GUI controls
println(Serial.list()); // print the comm ports to the debug window for debugging purposes
// make a listbox and populate it with the available comm ports
commListbox = controlP5.addListBox("myList",5,25,120,120); //addListBox(name,x,y,width,height)
commListbox.captionLabel().toUpperCase(false);
commListbox.captionLabel().set("Listbox label");
for(int i=0;i<Serial.list().length;i++) {
commListbox.addItem("port: "+Serial.list()[i],i); // addItem(name,value)
}
// text label for which comm port selected
txtlblWhichcom = controlP5.addTextlabel("txtlblWhichcom","No Port Selected",150,25); // textlabel(name,text,x,y)
ControlGroup cg = controlP5.addGroup("LED CONTROL",230,230);
//cg.moveTo(cw);
controlP5.begin(cg,0,10);
controlP5.addSlider("BRIGHTNESS",0,255).linebreak();
controlP5.addToggle("ON/OFF");
controlP5.end();
}
void draw() {
background(128);
}
// print the name of the control being triggered (for debugging) and see if it was a Listbox event
public void controlEvent(ControlEvent theEvent) {
// ListBox is if type ControlGroup, you need to check the Event with if (theEvent.isGroup())to avoid an error message from controlP5
if (theEvent.isGroup()) {
// an event from a group
if (theEvent.name()=="myList") {
InitSerial(theEvent.group().value()); // initialize the serial port selected
//println("got myList"+" value = "+theEvent.group().value()); // for debugging
}
}
int x=0,y=0;
if(theEvent.controller().name()=="ON/OFF") {
x=int (theEvent.controller().value());
if(x==1){
myPorts[0].write('E'); // ON LED
if(theEvent.controller().name()=="BRIGHTNESS") {
y= int (theEvent.controller().value());
myPorts[0].write(y);
}
}
else {
myPorts[0].write('A');
}
}
}
// initialize the serial port selected in the listBox
void InitSerial(float portValue) {
println("initializing serial " + int(portValue) + " in serial.list()"); // for debugging
String portPos = Serial.list()[int(portValue)]; // grab the name of the serial port
txtlblWhichcom.setValue("COM Initialized = " + portPos);
myPorts[0] = new Serial(this, portPos, 9600); // initialize the port
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPorts[0].bufferUntil('\n');
println("done init serial");
}
- void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(9, OUTPUT);
}
void loop() {
int brightness[2];
// check if data has been[] sent from the computer:
if (Serial.available()>0) {
// read the most recent byte (which will be from 0 to 255):
brightness[0] = Serial.read();
brightness[1] = Serial.read();
while(brightness[0]=='E'){
analogWrite(9, brightness[1]);// set the brightness of the LED:
}
}
}
version of controlP5 (0.5.7)
2