DC motor met Normally Closed eindschakelaars

Arduino specifieke Software
Berichten: 1
Geregistreerd: 24 Jun 2019, 10:56

DC motor met Normally Closed eindschakelaars

Berichtdoor Carlijnob » 24 Jun 2019, 11:03

Hoi,

Ik ben momenteel bezig met het produceren van een tafel die een zit/sta mechanisme krijgt ingebouwd (aangedreven door motor 1). Als de tafel op de hoogste stand is gaat het tafelblad bewegen (door motor 0). Hiervoor gebruik ik twee DC motoren, twee eindschakelaars die allen zijn aangesloten op een arduino met een monster moto shield van sparkfun. De code voldeed, tot de metaalbewerker besloot om de eindschakelaars te laten fungeren door middel van de optie Normally closed.
Wie o wie kan mij helpen om de code hieraan aan te passen?

Alle suggesties zijn welkom

Hoe het zou moeten werken:

before loop starts:
switch 2 is unpressed (it is Normally closed)
switch 1 is pressed (it is Normally closed)
motor 1 turns ON, CW (when motor 1 starts, switch 2 presses)

loop

if switch 1 unpresses:
motor 1 turns OFF
motor 2 turns ON CW for a random time period

if the random time period is over:
motor 2 turns OFF
motor 1 turns ON CCW

if switch 2 unpresses:
motor 1 turns OFF for a random time period

---
De code:

Code: Alles selecteren
/*  MonsterMoto Shield Example Sketch
  date: 5/24/11
  code by: Jim Lindblom
  hardware by: Nate Bernstein
  SparkFun Electronics

  License: CC-SA 3.0, feel free to use this code however you'd like.
  Please improve upon it! Let me know how you've made it better.

  This is really simple example code to get you some basic
  functionality with the MonsterMoto Shield. The MonsterMote uses
  two VNH2SP30 high-current full-bridge motor drivers.

  Use the motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
  function to get motors going in either CW, CCW, BRAKEVCC, or
  BRAKEGND. Use motorOff(int motor) to turn a specific motor off.

  The motor variable in each function should be either a 0 or a 1.
  pwm in the motorGo function should be a value between 0 and 255.
*/
#define BRAKEVCC 0
#define CW   1
#define CCW  2
#define BRAKEGND 3
#define CS_THRESHOLD 100

/*  VNH2SP30 pin definitions
  xxx[0] controls '1' outputs
  xxx[1] controls '2' outputs */
int inApin[2] = {7, 4};  // INA: Clockwise input
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
int pwmpin[2] = {5, 6}; // PWM input
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)


int statpin = 13;

//ButtonDebounce button1(7, 50);
//ButtonDebounce button2(4, 100);

int switch1pin=11;   // to store on or off value
int switch2pin=12;   // to store on or off value

int switch1=0;   // to store on or off value
int switch2=0;   // to store on or off value

long initialTime = 0;
long diffTime = 0;
boolean countingTime = false;
boolean switchTouching = false;

const int numberOfTimes = 3;
int randomTimes [numberOfTimes] = {20000, 16000,40000};
int randomNumber = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(switch1pin, INPUT);
  pinMode(switch2pin, INPUT);
  pinMode(statpin, OUTPUT);

  // Initialize digital pins as outputs
  for (int i = 0; i < 2; i++)
  {
    pinMode(inApin[i], OUTPUT);
    pinMode(inBpin[i], OUTPUT);
    pinMode(pwmpin[i], OUTPUT);
  }
  // Initialize braked
  for (int i = 0; i < 2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
motorGo(1, CW, 200);
randomNumber = random(0,numberOfTimes);
  Serial.print("random time =");
  Serial.println(randomTimes[randomNumber]);

}


void loop(){


switch1=digitalRead(switch1pin);
delay(10);
if(switch1 == HIGH && switchTouching == false){ // the switches are now working the other way around, thus they are being LOW when motor1 needs to turn off, how does that work for these lines that are controlling the switches?

  switchTouching = true;
   motorGo(1, CW, 0); //motorOff(1);
  Serial.println("motor 1 Off because of switch 1");
  Serial.println("wait 3 seconds");
  delay(3000);
  Serial.println("motor 0 On");
  motorGo(0, CW, 500);
  motorGo(1, CW, 0); //motorOff(1);

  initialTime = millis();
  countingTime = true;
}

  diffTime = millis() - initialTime;
  if( diffTime >= randomTimes[randomNumber] && countingTime == true){
      Serial.print(randomTimes[randomNumber]);
      Serial.println(" seconds since timing");
      Serial.println("motor 0 Off");
   
     motorGo(0, CW, 0); // motorOff(0);

      Serial.println("wait 1 second");

    delay(1000); 
      Serial.println("motor 1 On");
    motorGo(1, CCW, 200);
    delay(1000);
    countingTime = false;
      switchTouching = false;
  }
  switch2=digitalRead(switch2pin);
  delay(10);
  if(switch2 == HIGH && switchTouching == false){
  switchTouching = true;
      Serial.println("motor 1 Off because of switch 2");
    motorGo(1, CW, 0); // motorOff(1);
    randomNumber = random(0,numberOfTimes);
    delay(randomTimes[randomNumber]);
    switchTouching = false;
      Serial.println("wait for ");
      Serial.print(randomTimes[randomNumber]);
      Serial.println(" seconds");
     
    motorGo(1, CW, 200);
    randomNumber = random(0,numberOfTimes);

  }


}

void motorOff(int motor)
{
  // Initialize braked
   for (int i = 0; i < 2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  analogWrite(pwmpin[motor], 0);
}

/* motorGo() will set a motor going in a specific direction
  the motor will continue going in that direction, at that speed
  until told to do otherwise.

  motor: this should be either 0 or 1, will selet which of the two
  motors to be controlled

  direct: Should be between 0 and 3, with the following result
  0: Brake to VCC
  1: Clockwise
  2: CounterClockwise
  3: Brake to GND

  pwm: should be a value between ? and 1023, higher the number, the faster
  it'll go
*/
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
  if (motor <= 1)
  {
    if (direct <= 4)
    {
      // Set inA[motor]
      if (direct <= 1)
        digitalWrite(inApin[motor], HIGH);
      else
        digitalWrite(inApin[motor], LOW);

      // Set inB[motor]
      if ((direct == 0) || (direct == 2))
        digitalWrite(inBpin[motor], HIGH);
      else
        digitalWrite(inBpin[motor], LOW);

      analogWrite(pwmpin[motor], pwm);
    }
  }
}

Advertisement

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: DC motor met Normally Closed eindschakelaars

Berichtdoor shooter » 24 Jun 2019, 22:11

het handigste is met #define INON LOW
en #define INOFF HIGH te werken
zelfde voor uitgangen OUTON LOW OUTOFF HIGH.
als je dat bovenaan definieert dan kun je in je hele programma inon gebruiken etc. dan snapt iedereen wat er bedoeld is.
paul deelen
shooter@home.nl

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Richardhip en 9 gasten