Simultaneous control of lights and motor with Arduino using command from laptop

Good afternoon everyone,

I am a beginner to the Arduino environment and I am trying to build a small prototype for underwater ROV.

I have been researching online to find out how I can control LED lights and a motor simultaneously with Arduino, by applying the commands via laptop keyboard. However, I couldn't find what I wanted.

I am able to run the two sketches separately, but when I combine them together I need to click a key several times before the motors start/stop or the LEDs are on/off. So it does not happen with one click.

I imagine this has something to do with the data that Arduino reads when I press the buttons (it probably goes from 0 to 1 and the opposite).

Is there any example of how control two different things at the same time using the keyboard?

Thanks.

pzas21:
I am able to run the two sketches separately, but when I combine them together I need to click a key several times before the motors start/stop or the LEDs are on/off. So it does not happen with one click.

You need to post your program so we can see exactly what you are doing.

In the meantime these links may help
Serial Input Basics - simple reliable non-blocking ways to receive data.
Several Things at a Time

...R

Hi Robin,

Thank you very much for the quick reply. I will have a look into the links you sent.

See the code I am using below:

/
//  Inputs:
//  Keyboard O   : Turn lights ON
//  Keyboard C   : Turn Lights OFF
//  Keyboard R   : Turn motor clockwise
//  Keyboard L   : Turn motor anti-clockwise
//  Keyboard S   : Stop motor
//
//  Outputs:
//  Light Right          : digital
//  Light Left           : digital
//  MotorHL in1          : PWM
//  MotorHL in2          : PWM
//  MotorHL enable       : PWM (usually digital)



//////////
// PINS //
//////////

// Inputs

//// Motors

//// Horizontal Left
int pinMotHL1        = 11; // PWM
int pinMotHL2        = 12; // PWM
int pinMotHLEn       = 13; // PWM

// Lights
int pinLightL1       = 23; // digital
int pinLightL2       = 25; // digital
int pinLightLEn      = 27; // digital
int pinLightR1       = 22; // digital
int pinLightR2       = 24; // digital
int pinLightREn      = 26; // digital

// VARIABLES

int data; // initial value of input
char val;



///////////
// SETUP //
///////////

void setup()
{
  pinMode(pinLightL1, OUTPUT);
  pinMode(pinLightL2, OUTPUT);
  pinMode(pinLightLEn, OUTPUT);
  pinMode(pinLightR1, OUTPUT);
  pinMode(pinLightR2, OUTPUT);
  pinMode(pinLightREn, OUTPUT);
  pinMode(pinMotHL1, OUTPUT);
  pinMode(pinMotHL2, OUTPUT);
  pinMode(pinMotHLEn, OUTPUT);

  Serial.begin(9600);


}

//////////
// LOOP //
//////////

void loop() {
  light();
  motor();
}


void light() {

  /////////////////
  // READ INPUTS //
  /////////////////

  if (Serial.available()>0) // if serial value is available
  {
    char val = Serial.read(); //then read serial value
    Serial.print("You typed: " );
    Serial.println(val);
    Serial.println(data);

    ///LIGHTS///
    if (val == 'o')
    {
      digitalWrite(pinLightL1, HIGH);
      digitalWrite(pinLightL2, LOW);
      analogWrite(pinLightLEn, 255);

      digitalWrite(pinLightR1, HIGH);
      digitalWrite(pinLightR2, LOW);
      analogWrite(pinLightREn, 255);
      
    }
    else
    { if (val = 'c')
      {
        digitalWrite(pinLightL1, LOW);
        digitalWrite(pinLightL2, LOW);
        //        analogWrite(pinLightLEn, 0);
        //
        digitalWrite(pinLightR1, LOW);
        digitalWrite(pinLightR2, LOW);
        analogWrite(pinLightREn, 0);
        
      }
    }
  }
}

///LIGHTS///

///MOTOR///
void  motor () {
  if (Serial.available()>0) // if serial value is available
  {
    char val = Serial.read(); //then read serial value
    Serial.print("You typed: " );
    Serial.println(val);

    switch (val) {
      case  'r':
        analogWrite(pinMotHL1, 255);
        analogWrite(pinMotHL2, 0);
        analogWrite(pinMotHLEn, 255 * val);
        data = 1;
        break;
      case 'l':
        analogWrite(pinMotHL1, 0);
        analogWrite(pinMotHL2, 255);
        analogWrite(pinMotHLEn, -255 * val);
        data = 1;
        break;
      case 's':
        analogWrite(pinMotHL1, 0);
        analogWrite(pinMotHL2, 0);
        analogWrite(pinMotHLEn, 0);
        data = 0;
        break;
    }
  }
}

My intention is to develop the code so I can control 4 motors, lights and camera using Arduino.

At this point, I am looking if I can do it via laptop. Maybe in the future, I could try it using a joystick.

Thanks.

It might make more sense to read the value from Serial once at the top of loop() and THEN call routines which just deal with whatever has arrived.

Steve

I agree with Reply #3. I think it is also pretty much essential to send the two values (lights and motor) at the same time so the Arduino always knows which one is which. If you send them separately they could get mixed up. This may mean sending the same value over and over if it does not change.

...R

slipstick:
It might make more sense to read the value from Serial once at the top of loop() and THEN call routines which just deal with whatever has arrived.

Steve

Hi Steve,

Thanks for the reply. I will implement the change as advised.

Robin2:
I agree with Reply #3. I think it is also pretty much essential to send the two values (lights and motor) at the same time so the Arduino always knows which one is which. If you send them separately they could get mixed up. This may mean sending the same value over and over if it does not change.

…R

Hi Robin, thanks that as well.

I think I found a solution for working simultaneously. Maybe it is not the most appropriate one but it seems to work. I just combined both lights and motor in one function and used switch() { case:… break;} for applying every case that I am looking to have and it is working good!