MotorLights Code

By Eeric82

Dc motor controlled by Arduino with led speed indicator lights and automatic shutdown overload protection

int motorPin = 9;
int potPin = 3;
int sensorPin = A0;
int speedPin = A2;
int myledPins[] = {4, 5, 6, 7, 8};
int pinCount = 5;


void setup(){ 

Serial.begin(9600);

 for(int i=0;i<pinCount;i++)
    {pinMode(myledPins[i], OUTPUT);}
    {pinMode(potPin, OUTPUT);} 
    {pinMode(motorPin, OUTPUT);}
    {analogRead,(sensorPin,INPUT);}    
    {analogRead,(speedPin,INPUT);}}

void loop(){
  
  int speedVal=analogRead(speedPin);
      speedVal=map(speedPin, 0, 1023, 0, 255);
  
  int sensorVal = analogRead(sensorPin);
      sensorVal = map(sensorPin, 0, 1023, 0, 255);
      float voltage = sensorVal*(5.0/1023.0);    

{digitalWrite(potPin,HIGH);}

{ analogRead(sensorPin);}
   
 {Serial.println(voltage);
  Serial.print("sensorVal: ");
  delay(100);}  
   
 if(voltage == 0 || voltage == 5)
    {digitalWrite(motorPin,LOW);}
else{digitalWrite(motorPin,HIGH);}
   
  if  (voltage > 0 && voltage <=5)
       {digitalWrite(myledPins[0],HIGH);}
else   {digitalWrite(myledPins[0],LOW);}      
   
    if (voltage > 1 && voltage <=5)
         {digitalWrite(myledPins[1],HIGH);}
else     {digitalWrite(myledPins[1],LOW);}
    
     if (voltage > 2 && voltage <= 5)
          {digitalWrite(myledPins[2],HIGH);}
else      {digitalWrite(myledPins[2],LOW);}
           
      if (voltage > 3 && voltage <= 5)
           {digitalWrite(myledPins[3],HIGH);}
else       {digitalWrite(myledPins[3],LOW);}
     
       if (voltage > 4 && voltage <= 5)
            {digitalWrite(myledPins[4],HIGH);}
else        {digitalWrite(myledPins[4],LOW);}
     
 if (voltage == 5)
    {flash() ,myledPins[5];}
  
 }
    
   
    
  //*************LOOP END*************
  
  void flash()
    {int x = myledPins[5];
    
      digitalWrite(x , HIGH);
    delay(1);
    digitalWrite,(x , LOW);
    delay(1);(flash());}
  
void lightemUp(){
    
    int speedVal=analogRead(speedPin);
      speedVal=map(speedPin, 0, 1023, 0, 255);
  
  int sensorVal = analogRead(sensorPin);
      sensorVal = map(sensorPin, 0, 1023, 0, 255);
      float voltage = sensorVal*(5.0/1023.0);    

if  (voltage > 0 && voltage <=5)
        {digitalWrite(myledPins[0],HIGH);}
else    {digitalWrite(myledPins[0],LOW);}      
   
    if (voltage > 1 && voltage <=5)
         {digitalWrite(myledPins[1],HIGH);}
else     {digitalWrite(myledPins[1],LOW);}
    
     if (voltage > 2 && voltage <= 5)
          {digitalWrite(myledPins[2],HIGH);}
else      {digitalWrite(myledPins[2],LOW);}
           
      if (voltage > 3 && voltage <= 5)
           {digitalWrite(myledPins[3],HIGH);}
else       {digitalWrite(myledPins[3],LOW);}
     
       if (voltage > 4 && voltage <= 5)
            {digitalWrite(myledPins[4],HIGH);}
else        {digitalWrite(myledPins[4],LOW);}
     
        if (voltage == 5)
    {flash() ,myledPins[5];}(lightemUp());}

void motorsOn(){
  { int speedVal=analogRead(speedPin);
      speedVal=map(speedPin, 0, 1023, 0, 255);float voltage = sensorVal*(5.0/1023.0);    
  int pwm = speedVal;
   int sensorVal =      
        analogRead(sensorPin);
  pwm = map(motorPin, 0, 1023, 0, 255); 
  {
    analogRead(voltage);
  }
  if (voltage > 0)
  {analogWrite(motorPin, pwm);
  pwm++ ;}
  else
    if (voltage >= 1)
  {analogWrite(motorPin, pwm);
  pwm+=2 ;}
   else
    if (voltage >= 2)
       {analogWrite(motorPin, pwm);
  pwm+=4;}
    else
     if (voltage >= 3)
        {analogWrite(motorPin, pwm);
  pwm+=8;}
    else
    if (voltage >= 4)
       {analogWrite(motorPin, pwm);
  pwm+=16;}
    else
     if (voltage == 5)
        {analogWrite(motorPin, pwm);}
 pwm+=32;(motorsOn());}}