Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Theoretical based Question on the delay() function on: March 06, 2013, 12:25:08 pm
Ok thanks for the quick replies guys, here's my code.


Code:
const int X_Axis_Pin = A0;      //analog pin 0
const int Y_Axis_Pin = A1;      //analog pin 1
const int soft_Pot_Pin = A2;    //analog pin 2
const int DIR_PIN = 8;          //stepper motor direction pin
const int STEP_PIN = 9;         //stepper
const int magnet_Pin = 10;      //magnet pin
const int button_1_Pin = 2;     // the number of the pushbutton pin
const int button_2_Pin = 3;     // the number of the pushbutton pin
const int button_3_Pin = 4;     // the number of the pushbutton pin
const int button_4_Pin = 5;     // the number of the pushbutton pin
const int button_5_Pin = 6;     // the number of the pushbutton pin
const int button_6_Pin = 7;     // the number of the pushbutton pin
const int valve_1_Pin = 11;     // output to control valve
const int valve_2_Pin = 12;     // output to control valve
const int valve_3_Pin = 13;     // output to control valve
const int valve_4_Pin = A5;     // output to control valve
const float speed_Change = .05; // increment/decrement of Speed
const int time = 1000;
const int scaling_factor = 540/time; //Maximum distance from center divided by time


int X_Axis_Reading;
int Y_Axis_Reading;
int soft_Pot_Reading;
int button_1_State = 0;         // variable for reading the pushbutton status
int button_2_State = 0;         // variable for reading the pushbutton status
int button_3_State = 0;         // variable for reading the pushbutton status
int button_4_State = 0;         // variable for reading the pushbutton status
int button_5_State = 0;         // variable for reading the pushbutton status
int button_6_State = 0;         // variable for reading the pushbutton status
int magnet_State;               // magnet on or off
float Speed = .5;               // stepper motor speed (.01 <= Speed <= 1)



void setup(){
  digitalWrite(X_Axis_Pin, HIGH); //enable pullup resistors
  digitalWrite(Y_Axis_Pin, HIGH);
  digitalWrite(button_1_Pin, HIGH);
  digitalWrite(button_2_Pin, HIGH);
  digitalWrite(button_3_Pin, HIGH);
  digitalWrite(button_4_Pin, HIGH);
  digitalWrite(button_5_Pin, HIGH);
  digitalWrite(button_6_Pin, HIGH);
  
  pinMode(DIR_PIN, OUTPUT);       //set outputs
  pinMode(STEP_PIN, OUTPUT);
  pinMode(valve_1_Pin, OUTPUT);
  pinMode(valve_2_Pin, OUTPUT);
  pinMode(valve_3_Pin, OUTPUT);
  pinMode(valve_4_Pin, OUTPUT);
  
  Serial.begin(9600);
}

void loop(){
  X_Axis_Reading = analogRead(X_Axis_Pin);
  Y_Axis_Reading = analogRead(Y_Axis_Pin);
  soft_Pot_Reading = analogRead(soft_Pot_Pin);
  button_1_State = digitalRead(button_1_Pin);         // variable for reading the pushbutton status
  button_2_State = digitalRead(button_2_Pin);         // variable for reading the pushbutton status  
  button_3_State = digitalRead(button_3_Pin);         // variable for reading the pushbutton status
  button_4_State = digitalRead(button_4_Pin);         // variable for reading the pushbutton status
  button_5_State = digitalRead(button_5_Pin);         // variable for reading the pushbutton status
  button_6_State = digitalRead(button_6_Pin);         // variable for reading the pushbutton status
  

  Serial.print("X =");
  Serial.println(X_Axis_Reading);
  Serial.print("Y =");
  Serial.println(Y_Axis_Reading);
  Serial.print("1 =");
  Serial.println(button_1_State);
  Serial.print("2 =");
  Serial.println(button_2_State);
  Serial.print("3 =");
  Serial.println(button_3_State);
  Serial.print("4 =");
  Serial.println(button_4_State);
  Serial.print("5 =");
  Serial.println(button_5_State);
  Serial.print("6 =");
  Serial.println(button_6_State);
  Serial.print("Speed =");
  Serial.println(Speed);
  Serial.print("Magnet State = ");
  Serial.println(magnet_State);
 
  
  if ((!button_2_State) && Speed <= 1 - speed_Change){
    Speed = Speed + speed_Change;  //Speed increment
    Serial.println("Speed Increment.");
  }

  if ((!button_5_State) && Speed >= .01 + speed_Change){
    Speed = Speed - speed_Change;  //Speed degrement
    Serial.println("Speed Decrement");
  }  
  
  if (!button_3_State){
    rotateDeg(1,Speed);    //rotate 1 degree at Speed
    Serial.println("Forward Step");
  }
  
  if (!button_4_State){
    rotateDeg(-1, Speed);  //rotate -1 degree at Speed
    Serial.println("Reverse Step");
  }
  
  if (!button_6_State){
    //Nothing. insert automation loop here
    Serial.println("Auto Program");
  }
  
  if (!button_1_State){
    if (magnet_State == 1){
      magnet_State = 0;
      digitalWrite(magnet_Pin, LOW);
    }
    else{
      magnet_State = 1;
      digitalWrite(magnet_Pin, HIGH);
      delay(400);                      //to prevent uncontrolable and apparently random toggling.
      }
    Serial.println("Magnet Toggle");
  }
  
  if (Y_Axis_Reading >= 568){
   digitalWrite(valve_1_Pin, HIGH);
   Serial.println("Before");
   delay(time-((Y_Axis_Reading-559)/scaling_factor));
   Serial.println("During");
   digitalWrite(valve_1_Pin, LOW);
   delay((Y_Axis_Reading-559)/scaling_factor);
   Serial.println("After");
   }
 
  /*if (Y_Axis_Reading <= 550){
    digitalWrite(valve_2_Pin, HIGH);
    delay(-(Y_Axis_Reading-559)/scaling_factor);
    digitalWrite(valve_2_Pin, LOW);
    delay(time-(-(Y_Axis_Reading-559)/scaling_factor));
 }
  
  if (X_Axis_Reading >= 555){
    digitalWrite(valve_3_Pin, HIGH);
   delay(time-((X_Axis_Reading-547)/scaling_factor));
    digitalWrite(valve_3_Pin, LOW);
   delay((X_Axis_Reading-547)/scaling_factor);
   }
 
  if (X_Axis_Reading <= 535){
    digitalWrite(valve_4_Pin, HIGH);
   delay(-(X_Axis_Reading-547)/scaling_factor);
    digitalWrite(valve_4_Pin, LOW);
    delay(time-(-(X_Axis_Reading-547)/scaling_factor));
   }*/
  
  delay(1000); //just here to slow down the output for easier reading
 }
 

 

/*void rotate(int steps, float speed){
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  digitalWrite(DIR_PIN,dir);

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(usDelay);

    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(usDelay);
  }
}*/

void rotateDeg(float deg, float speed){
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir);

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(usDelay);

    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(usDelay);
  }
}

My apparent views of the delay() function must be flawed. The part of the code in consideration is everything including and below: (notice the if statements returning a negative value are commented out)

if (Y_Axis_Reading >= 568){
   digitalWrite(valve_1_Pin, HIGH);
   Serial.println("Before");
   delay(time-((Y_Axis_Reading-559)/scaling_factor));
   Serial.println("During");
   digitalWrite(valve_1_Pin, LOW);
   delay((Y_Axis_Reading-559)/scaling_factor);
   Serial.println("After");
   }
2  Using Arduino / Programming Questions / Theoretical based Question on the delay() function on: March 06, 2013, 11:41:06 am
I've never really needed to know the answer till now, and I couldn't find reading material on the matter so here's my question...

Can the delay() function in Arduino 1.0.3 be negative?

For certain instances, the way my delay function must be coded, the value inside the delay function can be negative. I don't return any errors when verifying the sketch so does the program automatically take the absolute value since obviously it can't delay a negative amount of time?
3  Using Arduino / Project Guidance / Re: Peak-and-Hold Circuit using PWM on: June 07, 2012, 08:46:11 am
cool project you got there.
4  Using Arduino / Project Guidance / Peak-and-Hold Circuit using PWM on: June 06, 2012, 02:25:30 pm
Hello everyone,

I was wondering if anybody could point me to some literature/tutorials on using the Arduino Uno to create a Peak-and-Hold Circuit that would turn a solenoid on and off. I was thinking about using the IRF640 for the switch, but other than that I'm free for suggestions. I have multiple inductors at hand as well as voltage supply sources and resistors.

I'm currently interested in a P+H Turn on aspect while involving a reverse current application to turn off the current flow, thus turning off the magnetic field in the system.

Any feedback is great, thanks.
5  Using Arduino / Project Guidance / Re: Using Arduino Uno to power an excavator arm via XBOX360/PS3/Wii controller on: June 01, 2012, 07:57:15 am


I currently have these joysticks that I could use to control the open/close of the solenoids. They are manufactured to be closed in the neutral position. It would simply be a forward/backward motion to control flow. I just don't really see then where the arduino could be integrated. I guess I wanted the design to be a little more complex.
6  Using Arduino / Project Guidance / Re: Using Arduino Uno to power an excavator arm via XBOX360/PS3/Wii controller on: May 31, 2012, 10:12:56 pm
Ok cool, would a joystick setup be even easier? I could eliminate even the use of the arduino with using the basic joysticks, but my hopes were to include the arduino in my project.
7  Using Arduino / Project Guidance / Using Arduino Uno to power an excavator arm via XBOX360/PS3/Wii controller on: May 31, 2012, 11:35:29 am
I have an excavator arm that is anchored down to a box to serve as its base. It currently is controlled by 4
manually operated valves that in turn move 4 pneumatic double acting cylinders. I am trying to switch the valves over to this valve which is controlled by a moving solenoid.

http://www.clippard.com/store/display_details.asp?sku=MME-42QEEC-W012#

I was wondering if anyone here could point me in the right direction for tutorials/code that shows how to control the solenoid valves by using a remote control. I have the arduino uno, access to Simulink, Matlab, LabVIEW, Arduino.exe and many transistors/resistors/wires/bread boards/etc

Here are some current photos of the excavator arm and base it sits on with the valves and hoses.





Again, currently this is a purely mechanical setup driven only by changes in pressure when the operator moves the nob up and down. I want to be able to press a button or move a joystick on any type of gaming controller to tell the cylinders on the arm to move up and down. Any help is greatly appreciated. I am not receiving any monetary wealth from this project, it is purely an educational project for kids interested in building things.
Pages: [1]