RCU Forums - View Single Post - converting air to electric retracts.
View Single Post
Old 02-01-2014, 03:35 AM
  #37  
mustang493
Member
 
Join Date: Jan 2013
Location: cambridge, UNITED KINGDOM
Posts: 42
Likes: 0
Received 0 Likes on 0 Posts
Default

Mike try this. i'm sure you know this but if you've been copying the code in these threads by hand into the arduino interface you can just copy all the below and paste it into the arduino interface, saves typing...



//this version designed for Mike.




#define midlimit 1700//pwm signal from the reciever has a value usually ranging from 600us to 2400us so when switch is in the middle the value would be around 1500. 1700 is inbetween 2400 and 1500


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 (switchpin, OUTPUT);
}
void loop () {


int limitA = 700; //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, its just a variable value it has no pin assigned to it. 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 its just a variable value it has no pin assigned to it. We will use to make sure the motors stop once they reach overcurrent condition
ch5 = pulseIn(6, HIGH); //measures an interger value of the pwn signal from the reciever. so if you're using a 3 position switch it will probably be around 600, 1500, or 2400


if (ch5 < midlimit) { //this next part changes the integer value of the pwm signal into a digital HIGH LOW by comparing the pwm value with the midlimit value defined at the beginning of the script
digitalWrite(switchpin, HIGH);
}
else {
digitalWrite(switchpin, LOW);// end of pwm signal to on/off (HIGH/LOW) script
}
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
int motorBfinished = 0;
int motorAfinished = 0;


while (updwn == updwn2 && updwn == 1) { //This means while updwn is exactly equal to 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 (sensorValueB < limitB && iB==0) // this determines the direction is up and that the motor is not in overload
{
analogWrite (motorspdB,255);
digitalWrite (motorB,HIGH);
digitalWrite (Bbrake,LOW);
}
else
{
analogWrite (motorspdB,0); // the motor is in over load and so the motor stops and there is no more power on the motor
digitalWrite (Bbrake,LOW);
motorBfinished = 1;
iB++; // this adds 1 to the value of iB once the motor has finished. now when we re-enter the while loop the value of iB is no longer 0 and so the motor doesnt run. this value continues to count up each time through the loop until the loop is broken by a change in the switch state.
}
delay(200); // delay between motorB starting and MotorA starting
if (motorBfinished == 1 && sensorValueA < limitA && iA==0) // this determines the direction is up and that the motor is not in overload
{
analogWrite (motorspdA,255);
digitalWrite (motorA,HIGH);
digitalWrite (Abrake,LOW);
}
else
{
analogWrite (motorspdA,0); // the motor is in over load and so the motor stops and there is no more power on the motor
digitalWrite (Abrake,LOW);
iA++; // this adds 1 to the value of iA once the motor has finished. now when we re-enter the while loop the value of iA is no longer 0 and so the motor doesnt run. this value continues to count up each time through the loop until the loop is broken by a change in the switch state.
}
delay(100);


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(200);


}
while (updwn == updwn2 && updwn == 0) { //This means while updwn is exactly equal to 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 (sensorValueA < limitA && iA==0) // this determines the direction is up and that the motor is not in overload
{
analogWrite (motorspdA,255);
digitalWrite (motorA,HIGH);
digitalWrite (Abrake,LOW);
}
else
{
analogWrite (motorspdA,0); // the motor is in over load and so the motor stops and there is no more power on the motor
digitalWrite (Abrake,LOW);
motorAfinished = 1; // once the motor stops this variable is set to indicate tht the next motor can begin to run
iA++; // this adds 1 to the value of iB once the motor has finished. now when we re-enter the while loop the value of iB is no longer 0 and so the motor doesnt run. this value continues to count up each time through the loop until the loop is broken by a change in the switch state.
}
delay(200); // delay between motorB starting and MotorA starting
if (motorBfinished == 1 && sensorValueB < limitB && iB==0) // this determines the direction is up and that the motor is not in overload
{
analogWrite (motorspdB,255);
digitalWrite (motorB,HIGH);
digitalWrite (Bbrake,LOW);
}
else
{
analogWrite (motorspdB,0); // the motor is in over load and so the motor stops and there is no more power on the motor
digitalWrite (Bbrake,LOW);
iB++; // this adds 1 to the value of iA once the motor has finished. now when we re-enter the while loop the value of iA is no longer 0 and so the motor doesnt run. this value continues to count up each time through the loop until the loop is broken by a change in the switch state.
}
delay(100);


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(200);


}
}