Go Down

Topic: Project 9 extra challenge (Read 564 times) previous topic - next topic

arduino-newbie

I get the concept of the extra challenge question on project 9.  Activating the transistor using a PWM pin, and I have done that using the:

analogWrite(motorPin, 255); or 90 or 0 etc...

but how would I run that using a potentiometer through the Analog input? I would need to calculate the Analog input 0 - 1023(?) and then correlate that with a PWM 0 - 255,
I'm just a bit uncertain as to what to type in to program that. 

Does this make any sense?

Thanks

The challenge is:
Try hooking up a potentiometer to an analog input and use that to PWM the pin that controls the transistor. 


my current sketch code is:

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
switchState = digitalRead(switchPin);

if(switchState == HIGH) {
  analogWrite(motorPin, 90);
}
else{digitalWrite(motorPin, LOW);}
}


dannable

Have you done an analog read exercise?

https://www.arduino.cc/en/Tutorial/AnalogReadSerial

The potentiometer can return a value of 0 - 1023. The speed of the motor on PWM takes a value of 0 - 255, ie a quarter of the value of the analog read value.

In future please post any code between code tags. If you don't know how to do this then read the topic entitled 'How to use this forum'.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

arduino-newbie

Thanks for the reply, I just finished the analog exercise and I have tweaked my code a bit, I'm getting mixed results. 

I did read the 'How to use this forum' post, unfortunately it was after I had posted the code.  I will be sure to use the proper protocol going forward. 

I now am dividing the A0 by 4 and having the analogRead act on that number.  What is happening is that I'm only seeing vary small variances in the Serial window, on the upper end of the pot it reads 255, on the other end it reads 251.  I may be doing something wrong.

Code: [Select]

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int sensorValue = analogRead(A0/4);
Serial.println(sensorValue);
delay(1);
switchState = digitalRead(switchPin);

if(switchState == HIGH) {
  analogWrite(motorPin, sensorValue);
}
else{digitalWrite(motorPin, LOW);}
}

dannable

Close! You are reading the value of (pin A0 divided by four), rather than (pin A0) divided by four!

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

KLHL

#4
Dec 14, 2015, 03:46 pm Last Edit: Dec 14, 2015, 05:09 pm by KLHL
One way to do it, propably a better way available from someone else.

I added an extra transistor for the potentiometer and a capacitor 10µF that takes away the remaining current that's left in the circuit so the engine stops faster and smoother.

KLHL

#5
Dec 14, 2015, 03:56 pm Last Edit: Dec 14, 2015, 05:09 pm by KLHL
One way to do it, propably a better way available from someone else.

I added an extra transistor for the potentiometer and a capacitor 10µF that takes away the remaining current that's left in the circuit so the engine stops faster and smoother.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy