Hi,
I'm very much a newbie to the Arduino, so forgive me if I am asking brainless questions. I want to use a Mega2560 for controlling a project.
The project has 3 parts to it and it would be nice to be able to run it off a single board.
I now have a clear idea as to how to code the LED sequencing I want. (That's the first part)
The second part is a PWM speed controller for a DC motor, the circuit I am intending will be a simple TIP120 controlling the output to the motor.
The third part is single channel servo controller.
The code that I have used to date is copied below. I have not yet wired this to a board, the testing has been using the simulator at 123D.
If the simulator is accurate then the motor speed controller gives me slightly fluctuating voltages (and thus jerky motor speed. (=/- 10uv) The waveform seems slightly rounded and irregular
Is this a result of clumsy code and if so can anyone suggest anything better? Its a learning curve for me so any input is appreciated.
int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
const byte switch_up[2] = {3,5};
const byte switch_down[2] = {2,4};
const byte led_up[2] = {8,10};
const byte led_down[2] = {6,11};
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
for (byte i = 0; i < 2; i++) {
pinMode(switch_up[i], INPUT_PULLUP);
pinMode(switch_down[i], INPUT_PULLUP);
pinMode(led_up[i], OUTPUT);
pinMode(led_down[i], OUTPUT);
}
myservo.attach(12); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (byte i = 0; i < 2; i++) {
if (digitalRead(switch_up[i]) == LOW) {
digitalWrite(led_up[i], HIGH);
digitalWrite(led_down[i], LOW);
}
if (digitalRead(switch_down[i]) == LOW) {
digitalWrite(led_down[i], HIGH);
digitalWrite(led_up[i], LOW);
}
potValue = analogRead(potPin);
motorValue = map(potValue, 0, 1023, 0, 255);
analogWrite(motorPin, motorValue);
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("\t motor = ");
Serial.println(motorValue);
delay(2);
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
}