I need help?

Hello,

Im building a project that controlling the speed of a DC motor similar to this: http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads
(http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads). Also, adding a led bar to get the output of the potentionmeter.

I try to load the code so into the Arduino UNO but i could'nt get to work where as i increase or decrease the motor speed to show the output level on the led bar.

So how can I fix this to make my potentionmeter to respond my led bar, im still a noobie to coding and need help? Can someone put this two code together for me?

-increase or decrease speed;

const int transistorPin = 9;

void setup() {

pinMode(transistorPin, OUTPUT);
}

void loop() {

int sensorValue = analogRead(A0);

int outputValue = map(sensorValue, 0, 1023, 0, 255);

analogWrite(transistorPin, outputValue);
}

-Led bar;

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);
}
}
}

Try here

AWOL:
Try here

Thats the project im working on now but i would like to know if you can put together the code because when i try it dont work? Putting the code together for me would be a good help, please? :frowning:

Simplistically, it is this:
(Not compiled yet)

const int transistorPin = 9;   
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 

int sensorValue = 0;
int outputValue = 0;
int sensorReading = 0;
int ledLevel = 0;

 void setup() {
   
   pinMode(transistorPin, OUTPUT);

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

 }

 void loop() {
  
   sensorValue = analogRead(A0);
   
   outputValue = map(sensorValue, 0, 1023, 0, 255);
  
   analogWrite(transistorPin, outputValue);

  // read the potentiometer:
  sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  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); 
    }
  }
}

However, you will note that pin 9 is used twice - and you want to use it as a PWM output.
So you need to replace 9 with something else in the ledPins[] array. Perhaps 12 would be acceptable.

Slim112_itt-tech:
I try to load the code so into the Arduino UNO but i could'nt get to work where as i increase or decrease the motor speed to show the output level on the led bar.

Is it correctly reading the potentiometer value?
Is it correctly displaying it in the led bar?
Is it correctly controlling the motor speed?