Poll
Question: I need help programing a DC Motor with 3 switches
DC MOTOR - 0 (0%)
ALGORITHM - 0 (0%)
Total Voters: 0

Pages: [1]   Go Down
Author Topic: DC motor control with switches  (Read 2681 times)
0 Members and 1 Guest are viewing this topic.
San Jose
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Leggo My Eggo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I have 3 switches

(Switch 1: inspectionready, Switch 2:stopmotor, Switch 3:inspection stop)

so when switch 1 is pushed
    motor will rotate clockwise
if switch 2 is pushed stop motor
if switch 1 is pushed again
    motor will rotate clockwise
then motor stop

finally is switch 3 is pushed program will end.


***********i am having trouble with making switch one to make the motor turn clock wise
then stop when switch 2 is pressed
then when switch 1 is pressed again motor will turn counter clockwise
then stop when switch 2 is pressed********************

please help i am a newb...i have looked a bunch of examples....

const int inspectready = 2;  
const int stopmotor = 3;
const int inspectstop =  4;      
const int camera = 9;    
const int light =  10;      
const int motorcw = 11;    
const int motorccw =  12;    

// variables will change:
int startbutton = 0;         // variable for reading the pushbutton status
int inspectiondone = 0;  
int motorstop = 0;

void setup() {

  pinMode(inspectready, INPUT);
  pinMode(stopmotor, INPUT);  
  pinMode(inspectstop, INPUT);    
  pinMode(camera, OUTPUT);      
  pinMode(light, OUTPUT);
  pinMode(motorcw, OUTPUT);      
  pinMode(motorccw, OUTPUT);
}

void loop(){
  startbutton = digitalRead(inspectready);
  motorstop = digitalRead(stopmotor);
  inspectiondone = digitalRead(inspectstop);

  
while(inspectiondone == LOW){
while (startbutton = HIGH){    
   if(startbutton = HIGH){
    digitalWrite(camera, HIGH);
    digitalWrite(light, HIGH);  
    digitalWrite(motorcw, HIGH);  
  }
while (startbutton == HIGH) {
  if (startbutton == HIGH){
    digitalWrite(camera, HIGH);
    digitalWrite(light, HIGH);  
    digitalWrite(motorccw, HIGH);
  }
  else if (motorstop == HIGH) {
    digitalWrite(camera, LOW);
    digitalWrite(light, LOW);  
    digitalWrite(motorcw, LOW);
    digitalWrite(motorccw, LOW);
  }}
}}
}


* flow chart.jpg (52.45 KB, 957x699 - viewed 59 times.)

* flow chart.jpg (52.45 KB, 957x699 - viewed 34 times.)
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3225
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see one problem... you assume that LOW is 0 and probably that HIGH is 1. Although that may be right, you should be consistent in your code to prevent problems that are hard to track down.

so this:

Code:
int inspectiondone = 0;
and this:
Code:
while(inspectiondone == LOW){

May not give you the expected results. I know this is not the problem, it's just an advice.

Then the other thing I see is infinite loops... loads of them...

Code:
void loop(){  //you read the pins once...
  startbutton = digitalRead(inspectready);
  motorstop = digitalRead(stopmotor);
  inspectiondone = digitalRead(inspectstop);

 
while(inspectiondone == LOW){ //then if inspectiondone goes low, you have one infinite loop here...
while (startbutton = HIGH){      //then you assign startbutton and therefore, you'll have another infinite loop here---
   if(startbutton = HIGH){
    digitalWrite(camera, HIGH);
    digitalWrite(light, HIGH); 
    digitalWrite(motorcw, HIGH); 
  }
while (startbutton == HIGH) {//this is another infinite loop and a redundant one...
  if (startbutton == HIGH){
    digitalWrite(camera, HIGH);
    digitalWrite(light, HIGH); 
    digitalWrite(motorccw, HIGH);
  }
  else if (motorstop == HIGH) {
    digitalWrite(camera, LOW);
    digitalWrite(light, LOW); 
    digitalWrite(motorcw, LOW);
    digitalWrite(motorccw, LOW);
  }}
}}
}

Can you try this?

Code:
void loop(){

while((inspectiondone=digitalRead(inspectstop)) == LOW){
  if ((startbutton=digitalRead(inspectready)) == HIGH){
    digitalWrite(camera, HIGH);
    digitalWrite(light, HIGH); 
    digitalWrite(motorccw, HIGH);
  }
  else if ((motorstop= digitalRead(stopmotor)) == HIGH) {
    digitalWrite(camera, LOW);
    digitalWrite(light, LOW); 
    digitalWrite(motorcw, LOW);
    digitalWrite(motorccw, LOW);
  }}
}}
}

I did not understand your conditions to start and stop the motor... but I think you are complicating things way too much for what you described. Either way, you don't have infinite cycles in the code now.
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3639
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As bubulindo observes, your while loops are causing you issues, as written, your program will only read the buttons once. Assuming you weren't pressing the InspectionStop button when the program started, it will spin in the first loop, doing nothing, forever.

A more effective approach would be to build a state machine: you only need a few states - start, cw, stop, ccw, end inspection. Read the buttons at the start of loop & then use a switch statement based on current state to see if the buttons indicate a need for state change. If they do, set your outputs appropriately and change state. Then loop will be called repeatedly, rather than once as you have it. There are plenty of examples of this posted in the forums - I gave one in this thread: http://arduino.cc/forum/index.php/topic,59511.msg429751.html#msg429751
Logged

Pages: [1]   Go Up
Jump to: