Multiple coding

Hi I am very very new to this and would appreciate some guidance. Is it possible to have two bits of coding one arduino; one for an LED light and one for a motor?

Thanks,
Alice

Yes.

Thank you. How do you do it coding wise?

what do you want to do? why not watch some tutorials? Tutorial 01: Hardware Overview: Arduino Course for Absolute Beginners (ReM) - YouTube

What is the coding if I want to run a motor and an LED light? How am I able to program both in same arduino?

Can you blink an LED? What have you tried?

It's hard to be specific when you don't say what you want to LED and motor to actually DO... so I made up an example task: LED blinks on for a second and off for a second. Motor turns on for 2.35 seconds and off for 2.35 seconds. That should be enough to demonstrate that the code is working independently for the two devices.

// Blink an LED at one rate while turning a motor on and off
// at a different rate

// constants won't change. Used here to set a pin number :
const byte LED_PIN =  13; // 'L' LED on Arduino UNO
const byte MOTOR_PIN = 4;

// Variables will change :

// Time values
unsigned long LastLEDChangeTime;
unsigned long LastMotorChangeTime;

const unsigned long LED_INTERVAL = 1000; // milliseconds
const unsigned long MOTOR_INTERVAL = 2350; // milliseconds

void setup() {
  digitalWrite(LED_PIN, LOW);
  pinMode(LED_PIN, OUTPUT);

  digitalWrite(MOTOR_PIN, LOW);
  pinMode(MOTOR_PIN, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - LastLEDChangeTime >= LED_INTERVAL) {
    // save the last time you blinked the LED
    LastLEDChangeTime += LED_INTERVAL;

    // if the LED is off turn it on and vice-versa:
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  }

  if (currentMillis - LastMotorChangeTime >= MOTOR_INTERVAL) {
    LastMotorChangeTime += MOTOR_INTERVAL;

    // if the motor is off turn it on and vice-versa:
    digitalWrite(MOTOR_PIN, !digitalRead(MOTOR_PIN));
  }
}

Have a look at the demo Several Things at a Time or the more extensive example in Planning and Implementing a Program

...R

johnwasser:
It's hard to be specific when you don't say what you want to LED and motor to actually DO... so I made up an example task: LED blinks on for a second and off for a second. Motor turns on for 2.35 seconds and off for 2.35 seconds. That should be enough to demonstrate that the code is working independently for the two devices.

// Blink an LED at one rate while turning a motor on and off

// at a different rate

// constants won't change. Used here to set a pin number :
const byte LED_PIN =  13; // 'L' LED on Arduino UNO
const byte MOTOR_PIN = 4;

// Variables will change :

// Time values
unsigned long LastLEDChangeTime;
unsigned long LastMotorChangeTime;

const unsigned long LED_INTERVAL = 1000; // milliseconds
const unsigned long MOTOR_INTERVAL = 2350; // milliseconds

void setup() {
  digitalWrite(LED_PIN, LOW);
  pinMode(LED_PIN, OUTPUT);

digitalWrite(MOTOR_PIN, LOW);
  pinMode(MOTOR_PIN, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

if (currentMillis - LastLEDChangeTime >= LED_INTERVAL) {
    // save the last time you blinked the LED
    LastLEDChangeTime += LED_INTERVAL;

// if the LED is off turn it on and vice-versa:
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  }

if (currentMillis - LastMotorChangeTime >= MOTOR_INTERVAL) {
    LastMotorChangeTime += MOTOR_INTERVAL;

// if the motor is off turn it on and vice-versa:
    digitalWrite(MOTOR_PIN, !digitalRead(MOTOR_PIN));
  }
}

Thanks so much - this is really helpful

what is the layout on the arduino board for this coding?

what is the layout on the arduino board for this coding?

Do you mean the connections to the board ?

const byte LED_PIN =  13; // 'L' LED on Arduino UNO
const byte MOTOR_PIN = 4;

Pin 13 is the built in LED on a Uno.
Can you guess which pin the motor is connected to ? Don't forget that it also needs a connection to GND.

I have the LED long leg in at 13 and short leg in GND. But its still not working?

aliali44:
I have the LED long leg in at 13 and short leg in GND. But its still not working?

You have not said which board you have. Remove your LED and just use the built in one already on the board, if there is one, or change the LED pin number in the code, put the long leg of your LED on that pin and the other leg to GND via a 470 ohm resistor to limit the current to it.