Code for multiple DC motors with LED`s and a switch


hey, wrote , hoping please check , see if work.

when switch on, each motor , led should turn on every 2 seconds, 1 @ time. mean every time motor turns on, led turn on it, until 5 motors , led`s have been turned on. code supposed run once.

code: [select]

int motorpin1 = 1;
int ledpin1 = 2;
int motorpin2 = 3;
int ledpin2 = 4;
int motorpin3 = 5;
int ledpin3 = 6;
int motorpin4 = 7;
int ledpin4 = 8;
int motorpin5 = 9;
int ledpin5 = 10;
const int buttonpin = 2;
int buttonstate = 0;

void setup() {
  pinmode(buttonpin, input);
  pinmode(motorpin1, output);
  pinmode(motorpin2, output);
  pinmode(motorpin3, output);
  pinmode(motorpin4, output);
  pinmode(motorpin5, output);

  buttonstate = digitalread(buttonpin);

  if (buttonstate == high) {
    digitalwrite(motorpin1, high);
    digitalwrite(ledpin1, high);
    delay(2000);
  } else {
    digitalwrite(motorpin1, low);
    digitalwrite(ledpin1, low);
  }
  if (buttonstate == high) {
    digitalwrite(motorpin2, high);
    digitalwrite(ledpin2, high);
    delay(2000);
  } else {
    digitalwrite(motorpin2, low);
    digitalwrite(ledpin2, low);
  }

  if (buttonstate == high) {
    digitalwrite(motorpin3, high);
    digitalwrite(motorpin3, high);
    delay(2000);
  } else {
    digitalwrite(motorpin3, low);
    digitalwrite(ledpin2, low);
  }

  if (buttonstate == high) {
    digitalwrite(motorpin4, high);
    digitalwrite(ledpin4, high);
    delay(2000);
  } else {
    digitalwrite(motorpin4, low);
    digitalwrite(ledpin4, low);

  }

  if (buttonstate == high) {
    digitalwrite(motorpin5, high);
    digitalwrite(ledpin5, high);
    delay(2000);
  } else {
    digitalwrite(motorpin5, low);
    digitalwrite(ledpin5, low);
  }
}
void loop() {

}

probably not idea use pin 2 two different things or use pin 1 @ (depending on board it's for).

and need clear specification. @ moment looks motor/leds start 1 every 2 seconds , when they're on keep going ever (or until kill power).


but why don't test it? it's easy enough try leds connected @ first. if want can connect motors (but hope you're switching them mosfets, transistors or motor shields...you don't run motors direct arduino pins).

there easier ways of coding that. since read buttonstate once there's not point checking often. should something.

steve


Arduino Forum > Using Arduino > Programming Questions > Code for multiple DC motors with LED`s and a switch


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???