RCU Forums - View Single Post - converting air to electric retracts.
View Single Post
Old 01-30-2013, 01:57 PM
  #8  
mustang493
Member
 
Join Date: Jan 2013
Location: cambridge, UNITED KINGDOM
Posts: 42
Likes: 0
Received 0 Likes on 0 Posts
Default RE: converting air to electric retracts.

This is the code you'll need to program your Arduino UNO.  If you're familiar with code you'll get the idea.  if not i'll explain in later posts
cut and paste from #include<Servo.h......



#include <Servo.h> //needed to operate the servos
#define midlimit 1700
Servo myservoA;
Servo myservoB;

int Bbrake=8;//brake signal for motor B
int Abrake=9;//brake signal for motor A
int motorspdB=11;//PWM speed signal for motor B
int motorspdA=3;//PWM speed signal for motor A
int ch5;
int motorA=12;
int motorB=13;
int sensorPinA = A0;    // select the input pin for the current
int sensorPinB = A1;    // select the input pin for the current
int switchpin = 2;
int sensorValueA = 0;  // variable to store the value coming from the sensor
int sensorValueB = 0;  // variable to store the value coming from the sensor

void setup () {

  pinMode (6, INPUT);
  pinMode (13, OUTPUT);//motor B enable and direction HIGH or LOW
  pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
  pinMode (8, OUTPUT);//Brake motor B
  pinMode (9, OUTPUT);//Brake motor A
  pinMode (4, OUTPUT);//servo A pin
  pinMode (7, OUTPUT);//servo B pin 
  pinMode (switchpin, OUTPUT);
  myservoA.attach(4);
  myservoB.attach(7);
}
void loop () {

  int limitA = 550; //this is the current limit for motor A
  int limitB = 300; //this is the current limit for motor B
  sensorValueB = analogRead(sensorPinB); //this reads the value of current for motorB and sets the variable sensorValueB
  sensorValueA = analogRead(sensorPinA); //this reads the value of current for motorA and sets the variable sensorValueA
  int iB = 0; //this creates and sets a variable named iB which we will use to make sure the motors stop once they reach overcurrent condition
  int iA = 0; //this creates and sets a variable named iA which we will use to make sure the motors stop once they reach overcurrent condition
  myservoA.writeMicroseconds(900);
  myservoB.writeMicroseconds(900);
  ch5 = pulseIn(6, HIGH);

  if (ch5 < midlimit) {
    digitalWrite(switchpin, HIGH);
  }
  else {
    digitalWrite(switchpin, LOW);
  }
  int updwn = digitalRead(switchpin); //this sets the value of the variable updwn which is read from transmitter switch
  int updwn2 = digitalRead(switchpin); //this sets the comparison value of updwn which is read from transmitter switch

  while (updwn == updwn2) { //the while loop here knows the current state of the switch updwn. during the while loop it will continually check 
    //the state of the switch by setting updwn2 therefore once the state of the switch changes it will exit the while loop

    if (updwn==1 && sensorValueB < limitB && iB==0) 
    {
      analogWrite  (motorspdB,255);
      digitalWrite (motorB,HIGH);
      digitalWrite (Bbrake,LOW);
    }
    else if (updwn==0 && sensorValueB < limitB && iB==0) 
    {
      analogWrite  (motorspdB,255);
      digitalWrite (motorB,LOW);
      digitalWrite (Bbrake,LOW);
    }
    else 
    {
      analogWrite  (motorspdB,0);
      digitalWrite (Bbrake,LOW);
      myservoB.writeMicroseconds(2095);
      iB++;
    }
    delay(300);
    if (updwn==1 && sensorValueA < limitA && iA==0)
    {
      analogWrite  (motorspdA,255);
      digitalWrite (motorA,HIGH);
      digitalWrite (Abrake,LOW);
    }
    else if (updwn==0 && sensorValueA < limitA && iA==0) 
    {
      analogWrite  (motorspdA,255);
      digitalWrite (motorA,LOW);
      digitalWrite (Abrake,LOW);
    }
    else
    {
      analogWrite  (motorspdA,0);
      digitalWrite (Abrake,LOW);
      myservoA.writeMicroseconds(2125);
      iA++;
    } 
    delay(200);

    sensorValueB = analogRead(sensorPinB);
    sensorValueA = analogRead(sensorPinA);

    int ch5 = pulseIn(6, HIGH);
    if (ch5 < midlimit) 
    {
      digitalWrite(switchpin, HIGH);
    }
    else 
    {
      digitalWrite(switchpin, LOW);
    }
    updwn2 = digitalRead(switchpin);
    delay(100);

  }
}