0

i have been trying to set Builtin LED frequency from arduino and turn it on and off .

I have created an interface with knobs and buttons in processing . I want to set delay between ON and OFF with 1 knob and OFF and ON delay with another knob . And button blinks the led at given frequency

In arduino i have tried something like this :

if(Serial.available() >0){
char val=Serial.read();
switch(val)
{
  case 'c':
  theValue=Serial.read();
  break;
  case 'd':
  theValue1=Serial.read();
  break;

case 'r':
digitalWrite(13,HIGH);
delay(theValue);
digitalWrite(13,LOW);
delay(theValue1);

break;
}

}

In the processing :

  public void Blink(){
   myPort.write('r');
 }

 public void knobON(int theValue){
   myPort.write('c');
   myPort.write(theValue);
 }

 public void knobOFF(int theValue1){
   myPort.write('d');
   myPort.write(theValue1);
 }

Can you help me on what should i do ?

1
  • Do you really need to read char over serial? Why not send an number, i.e. integer?
    – MichaelT
    Jan 22, 2019 at 21:52

1 Answer 1

0

The Arduino code sample you've provided is incomplete, and does not compile. On the Processing side, I would combine the LED On time with the Off time, using a separator, then ending with a \n. Something like this: myPort.write("1000:2000\n");.

There are many ways to parse the serial data into unsigned long to be used in the delay() function. Here is a sketch illustrating one way to parse it. I am using the serial monitor to send the following test data 1000:2000 (LED on for 1 second, off for 2 seconds). The serial monitor is sending the \n character.

char buffer[16];
unsigned long onTime;
unsigned long offTime;

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop(){

  if(Serial.available() > 0){

    Serial.readBytesUntil('\n', buffer, 13);

    char * pch;
    pch = strtok(buffer, ":");
    onTime = strtol(pch, &pch, 10);
    pch = strtok(NULL, ":");
    offTime = strtol(pch, NULL, 10);

    digitalWrite(13, HIGH);
    delay(onTime);
    digitalWrite(13, LOW);
    delay(offTime);
    memset(buffer, 0, sizeof(buffer));
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.