Project BarGrapgh + DC Motor (power control)

I wanted to control the power of a DC motor and led at the same time with a potentiometer, while using a BarGrapgh (8 leds showing how much power).

So I have got two codes.

DC motor and led:

#define MOF_PIN 6
#define POT_PIN A1

int pot = 0;

void setup() {
  pinMode(MOF_PIN, OUTPUT);
  pinMode(POT_PIN, INPUT);
  Serial.begin(9600);
}

void loop() {
  pot = analogRead(POT_PIN);
  Serial.println(pot);
  
  analogWrite(MOF_PIN, pot/4);
}

BarGrapgh:

// these constants won't change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  // read the potentiometer:
  int sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
}

What would be the best way to combine these codes together to get it to work?

I have also attached a picture.

Thank you for your time and help!

[Edited because of my mistake! I have put the question back]

There is reading of a pot in both sketches, just into a different variable name. In the LED sketch, replace sensorReading ( in both places) with pot and put the motor code into loop(). And don’t forget the #define MOF_PIN 12. Untested code (compiles) below.

#define MOF_PIN 12

const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9,
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop()
{
  // read the potentiometer:
  int pot = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(pot, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel)
    {
      digitalWrite(ledPins[thisLed], HIGH);
    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
  // motor control
   analogWrite(MOF_PIN, pot/4);
}

A couple of links to information on combining sketches.

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

https://arduino-info.wikispaces.com/CombiningArduinoSketches

Thank you so much! It's working.

Could anyone delete this post please?

@ILoveDonuts,

The strength of a forum is that people leave their messages untouched. Now all the replies that you got for free that could have helped someone else in the future are responses on some question unknown. This is a pity.
Please don't do not remove your original question again for the sake of the next generation,

robtillaart:
@ILoveDonuts,

The strength of a forum is that people leave their messages untouched. Now all the replies that you got for free that could have helped someone else in the future are responses on some question unknown. This is a pity.
Please don't do not remove your original question again for the sake of the next generation,

Yes I am very sorry. I didn't realize that at that moment. I have learned a lot from it. This won't happen anymore! Thank you. I can put my original question back again if you would like. It might help others?
Tell me what you think. :slight_smile:

Please do as otherwise the only option left is to remove the whole thread .

Thanks for your understanding,
Rob