Hello, i have an issue. Basically task is to control DC motor with 2 buttons.
First button adds up motor speed, second button slows down motor.
Issue is there that when i plug buttons in board, voltage goes to max and i cant control DC motor, but when buttons are out of board, all works fine. I don't know where is the problem.
Thank you in advance.
CODE
int motor = 5;
int button1 = 7, button2 = 4;
boolean faster, slower;
int step =3;
int beginningspeed= 0;
int maxspeed =255;
As a test, try a small delay in your loop() say delay(100) to see if that helps slow the progression down.
As it is, it will reach 255 maximum very quickly.
So the way you position and connect them on the breadboard is wrong. For more detail post a clear picture showing the breadboard with the buttons and their connections.
6v6gt:
You have to rotate and rewire the buttons. They are currently shorted out.
Not quite correct. It is not just rotation.
The buttons were shorted out because the two jumper wires were inserted in the same column of the breadboard, which is five holes all connected together.
It sounds like jankeljs has figured out how to actually use the breadboard. Generally the buttons are inserted over the central valley so that each pin connects to a separate column of pins and if there is any doubt as to which pair of pins of the button are already connected together, you just connect as Perry says, to diagonally opposite pins.
Hello i have an issue, task is to control 12v DC motor.
With first button, you can add up motor speed by certain speed step.
With second button, you can slow down motors speed by certain speed step.
with third button you cant speed up motor to its max, but problem is there that i must be dome smoothly. For example i press third button and in motor starts to spin faster and faster till it reaches max speed. But now when i press this button motor jumps to it max, and i don't know why. There is no smooth going to max.
I cant use delay, i have tried to add additional milis, but it doesn't help
CODE
int speed;
int motor = 5,
fasterbutton = 7, slowerrbutton = 4, tillmaxbutton = 2;
unsigned long previousMillis = 0;
const long interval = 100;
unsigned long smoothMillis = 0;
const long smoothinterval = 5000;
boolean faster, slower, tillend;
int begginingspeed;
int step = 3;
int maxspeed = 255;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(7, INPUT_PULLUP);
speed = 0;
Serial.begin(9600);
}
void loop() {
tillend = digitalRead(2);
slower = digitalRead(4);
faster = digitalRead(7);
//poga Nr 7 iest un paatrina DC motoru
if (faster == !HIGH && begginingspeed < maxspeed) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
begginingspeed = begginingspeed + step;
if (faster == !HIGH && speed < 1 ) {
Serial.print('\n');
Serial.print("MOTOR ON" );
}
analogWrite(motor, begginingspeed);
Serial.print('\n');
Serial.print("Speed = " );
speed = begginingspeed * 100 / maxspeed;
Serial.print(speed, DEC);
previousMillis = currentMillis;
}
}
//poga Nr 4 palelina DC motoru
else if (slower == !HIGH && begginingspeed > 0) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
begginingspeed = begginingspeed - step;
analogWrite(motor, begginingspeed);
Serial.print('\n');
Serial.print("Speed = " );
speed = begginingspeed * 100 / maxspeed;
Serial.print(speed, DEC);
if (slower == !HIGH && speed < 1 ) {
Serial.print('\n');
Serial.print("MOTOR OFF" );
}
previousMillis = currentMillis;
}
}
//poga Nr 2 paatrina DC motoru lidz max
else if (tillend == !HIGH && begginingspeed > 0) {
unsigned long currentMillis = millis();
if (currentMillis - smoothMillis >= smoothinterval) {
smoothMillis = currentMillis;
}
for (begginingspeed = begginingspeed; begginingspeed <= maxspeed; begginingspeed++)
analogWrite(motor, begginingspeed);
speed = begginingspeed * 100 / maxspeed;
Serial.print(speed, DEC);
Serial.print('\n');
Serial.print("Sasniegts maksimālais ātrums = " );
Serial.print(speed, DEC);
}
}
well done. OK the basic concept of non-blocking timing based on function millis() is to compare actual time to certain-time-points.
If time-point has arrived then start action
explaining non-blocking-timing based on function millis():
imagine baking a frosted pizza
the cover says for preparation heat up oven to 200°C
then put pizza in baking time 10 minutes
You are estimating heating up needs 3 minutes
You take a look onto your watch it is 13:02
You start reading the newspaper and from time to time looking onto your watch
watch 13:02 not yet time
watch 13:03 not yet time
watch 13:04 not yet time
watch 13:05 when did I start 13:02 OK 13:05 - 13:02 = 3 minutes time to put pizza into the oven
New basetime 13:05
watch 13:06 not yet time
watch 13:07 not yet time
watch 13:08 not yet time
watch 13:09 not yet time
watch 13:10 not yet time
watch 13:11 not yet time
watch 13:12 not yet time
watch 13:13 not yet time
watch 13:14 not yet time
watch 13:15 when did I start 13:05 OK 13:15 - 13:05 = 10 minutes time to eat pizza (yum yum)
You did a repeated comparing how much time has passed by
This is what non-blocking timing does
This is what the if-condition
if (currentTime - previousTime >= Period)
is doing
So slowing down increasing motor-speed means you have a time until max-speed shall be reached
5 seconds. You have 255 steps for duty-cycle
so every 5000 milliseconds / 255 = 19.6 rounded 20 milliseconds dutycycle shall be increased by 1
You need an if-condition that evaluates to true once every 20 milliseconds and then increase dutycycle by 1
So now it is up to you to write an attempt how the code for this looks like
and then post the new code-version always as complete sketch.
Hello i have an issue, task is to control 12v DC motor.
With first button, you can add up motor speed by certain speed step.
With second button, you can slow down motors speed by certain speed step.
With third button you cant speed up motor to its max, but problem is there that i must be dome smoothly. For example i press third button and in motor starts to spin faster and faster till it reaches max speed. But now when i press this button motor jumps to it max, and i don't know why. There is no smooth going to max.
I cant use delay, i have tried to add additional milis, but it doesn't help
int speed;
int motor = 5,
fasterbutton = 7, slowerrbutton = 4, tillmaxbutton = 2;
unsigned long previousMillis = 0;
const long interval = 100;
const long smoothinterval = 5000;
const long millisstep = 20;
int millismototrstep = 5;
boolean faster, slower, tillend;
int begginingspeed;
int step = 3;
int maxspeed = 255;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(7, INPUT_PULLUP);
speed = 0;
Serial.begin(9600);
}
void loop() {
tillend = digitalRead(2);
slower = digitalRead(4);
faster = digitalRead(7);
//button Nr 7 start DC motor and festens up
if (faster == !HIGH && begginingspeed < maxspeed) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
begginingspeed = begginingspeed + step;
if (faster == !HIGH && speed < 1 ) {
Serial.print('\n');
Serial.print("MOTOR ON" );
}
analogWrite(motor, begginingspeed);
Serial.print('\n');
Serial.print("Speed = " );
speed = begginingspeed * 100 / maxspeed;
Serial.print(speed, DEC);
previousMillis = currentMillis;
}
}
//button Nr 4 slows down DC motor
else if (slower == !HIGH && begginingspeed > 0) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
begginingspeed = begginingspeed - step;
analogWrite(motor, begginingspeed);
Serial.print('\n');
Serial.print("Speed = " );
speed = begginingspeed * 100 / maxspeed;
Serial.print(speed, DEC);
if (slower == !HIGH && speed < 1 ) {
Serial.print('\n');
Serial.print("MOTOR OFF" );
}
previousMillis = currentMillis;
}
}
//button Nr 2 fastens up DC motor till max
else if (tillend == !HIGH && begginingspeed > 0) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= smoothinterval) {
begginingspeed;
if (currentMillis - previousMillis >= millisstep) {
for (begginingspeed = begginingspeed; begginingspeed < 255; begginingspeed = begginingspeed + millismototrstep)
analogWrite(motor, begginingspeed);
Serial.print('\n');
Serial.print(begginingspeed);
previousMillis = currentMillis;
}
previousMillis = currentMillis;
}
}
}