Pages: [1]   Go Down
Author Topic: code check for a noob?  (Read 947 times)
0 Members and 1 Guest are viewing this topic.
Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks & Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had help from a computer programmer because this was so far beyond any program I had done for Arduino. While the program DOES compile, i don't know that it is correct for arduino.  The machine isn't built yet to test. Could someone please check the code and help me make sure it should function as described below?

Description of function:
I have 2 pneumatic cylinders, one long controlled by valve#1 ,one short controlled by valve#2.  I have a reed switch to tell me when the long cylinder is at the top, and a photo eye to tell me when the load is at the top.
Long one uses repeated strokes to raise a load by a ratchet mechanism via a cord. It's "off" position will be extended fully, and "on" will be retracting.
It will take >2 but <3 strokes to reach the top (could vary) and must not continue to pull past the "top" position. Cyl will reverse if load isn't at top but cyl gets to bottom.
Valve #1 must switch as soon as sensor #2 detects the load at the top
Short cyl. releases the ratchet mechanism to let the load be lowered.

Quote
/*
UGBot Control program
Relay 1 controls the valve for the extension of air cyl 1 (long)
Relay 2 controls the valve for the extension of air cyl 2 (short)
Sensor 1 detects when the long cylinder is down
Sensor 2 detects when the load is at top
*/


const int v1 = 6 ;// relay controlling valve 1 for long stroke piston
const int v2 = 7 ;// relay controlling valve 2 for short stroke piston
const int s1 = 8 ;// sensor for cylinder at bottom position
const int s2 = 9 ;// sensor for load at top position

//variables for sensor states
int s1state = 0 ;
int s2state = 0 ;
int movingload = 0 ; // 0 = down  / 1 = up
int droptime = 5000;
int sleeptime = 1000;

void setup() {                
  pinMode(v1, OUTPUT); // relay 1 control
  pinMode(v2, OUTPUT); //relay 2 control
  pinMode(s1, INPUT); //sensor 1 at cyl bottom position
  pinMode(s2, INPUT); //sensor 2 at load top position

}

void loop() {

  //read sensors
  // s1state = digitalRead(s1);   //S1 High = Piston top
  // s2state = digitalRead(s2);   //S2 High = load Top


    //lower the load
  movingload = 0;

  digitalWrite(v2, HIGH);   //turn on relay, extend short cyl
  delay(droptime);
  digitalWrite(v2, LOW);    // turn off relay, retract short cyl
  delay(sleeptime);              // wait for a time

    movingload = 1;

  while (movingload = 1 )
  {
    while (s2 == LOW  && s1 == LOW)
    {
      digitalWrite(v1, HIGH) ; //
    }
    if (s1 == HIGH)  // Bottom of stroke
    {
      digitalWrite(v1, LOW);  // Re Extend
      delay(droptime);
    }
    if (s2 == HIGH) ; // Top of load
    {
      movingload = 0 ;
    }
  }
}



« Last Edit: February 02, 2011, 03:32:47 pm by Byron Morgan » Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need some fixes:
if (s2 == HIGH) ; // Top of load  << take out the semicolon here

You are missing digitalReads to get the state of the switches.
Well, I guess you have them there but they are commented out. You need your if comparisons to be against s1state and s2state.
The switches are normally closed (connected to ground), when activated they go open and the contact you are reading gets pulled up via pullup resistor?

You may need to move the reading of the switches as well, so you can make a decision after the moving has started for example and you can tell when it arrived at the other end.
For example
s1state = digitalRead(s1);
    if (s1 == HIGH)  // Bottom of stroke

Need to watch things like this too:
while (movingload = 1 )   << needs == for a comparison
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, semi removed.  I put int he digitalread based on some of the example code, i think button, but i'm not sure where exactly I need to have them. just where they are or every time i check to see their state? How will I constantly check to see if s1 goes high so that we can flip the valve and stop the cyl motion.

the switches are normally open, and I'll have them sending 5v thru to the input pins when they are closed.

Revised code below, with the obvious mistake of not knowing where the digitalread goes:

Quote
/*
UGBot Control program
 Relay 1 controls the valve for the extension of air cyl 1 (long) 
 Relay 2 controls the valve for the extension of air cyl 2 (short) 
 Sensor 1 detects when the long cylinder is down
 Sensor 2 detects when the load is at top
 */


const int v1 = 6 ;// relay controlling valve 1 for long stroke piston
const int v2 = 7 ;// relay controlling valve 2 for short stroke piston
const int s1 = 8 ;// sensor for cylinder at bottom position
const int s2 = 9 ;// sensor for load at top position

//variables for sensor states
int s1state = 0 ;
int s2state = 0 ;
int movingload = 0 ; // 0 = down  / 1 = up
int droptime = 5000;
int sleeptime = 1000;

void setup() {               
  pinMode(v1, OUTPUT); // relay 1 control
  pinMode(v2, OUTPUT); //relay 2 control
  pinMode(s1, INPUT); //sensor 1 at cyl bottom position
  pinMode(s2, INPUT); //sensor 2 at load top position

}

void loop() {

  //read sensors
  s1state = digitalRead(s1);   //S1 High = Piston top
  s2state = digitalRead(s2);   //S2 High = load Top


  //lower the load
  movingload = 0;

  digitalWrite(v2, HIGH);   //turn on relay, extend short cyl
  delay(droptime);
  digitalWrite(v2, LOW);    // turn off relay, retract short cyl
  delay(sleeptime);              // wait for a time

    movingload = 1;

  while (movingload == 1 )
  {
    while (s2 == LOW  && s1 == LOW)
    {
      digitalWrite(v1, HIGH) ; //
    }
    if (s1 == HIGH// Bottom of stroke
    {
      digitalWrite(v1, LOW);  // Re Extend
      delay(droptime);
    }
    if (s2 == HIGH// Top of load
    {
      movingload == 0 ;
    }
  }
}




Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so  your loop starts, you give valve 2 a little wiggle, you read the sensors, and you declare it to be moving.
Then you start a while (moving ==1) loop, and since you don't read the sensors anymore you have no way get out of it.
So, you need to add sxstate = digitalRead (sx) before your comparisons, and then do the comparisons against sxstate, not sx.
if (sxstate == high/low){
// then actions
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so do i need the s#state = digread(s#) before each time i refer to the position of that state?    I don't really understand where to put them to be sure that while v1 is on and the cyl is moving, that it will be stopped wherever it is once the s2 sees the load at the top limit (or repeats the motion if it doesn't see the load at the top, but s1 sees the cyl at the bottom instead)


I'm confused...admittedly through ignorance.  I haven't done enough yet to understand these things and don't get enough opportunity to stay sharp on what things i DO manage to learn.
Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
 while (movingload == 1 )
  {
   s1state = digitalRead(s1);
    s2state = digitalRead(s2);

    while (s2state == LOW  && s1state == LOW)
    {
      digitalWrite(v1, HIGH) ; //
    }
    if (s1state == HIGH)  // Bottom of stroke
    {
      digitalWrite(v1, LOW);  // Re Extend
      delay(droptime);
    }
    if (s2state == HIGH)  // Top of load
    {
      movingload = 0 ; // changed == to =
    }
  }
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. for my understanding sake, where does it pick up the change in s2 when it goes high and turn off v1?
Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having a little trouble following which valve makes it go up and which switch gets closed when it get there with all the scrolling up & down to read this thread.
Check your description, you may have the usages a little mixed up.

Code:
    if (s1state == HIGH)  // Bottom of stroke
    {
      digitalWrite(v1, LOW);  // Re Extend  << This indicates off due to reaching top??
      delay(droptime);
    }
    if (s2state == HIGH)  // Top of load
    {
     // probably want the write (v1, LOW) in here then.
      movingload = 0 ; // changed == to =
    }
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
where does it pick up the change in s2 when it goes high
Code:
const int s2 = 9 ;// sensor for load at top position
That's the beauty of const. s2 never changes.

It's really important that you distinguish between a pin and the state of that pin, and make it clear which one you are talking about.
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.

v1 (relay controlling the valve) goes ON, cyl retracts, load goes up, part way.
when cyl gets all the way to the bottom, s1 (reed switch) senses it.
s1 going ON tells v1 to go OFF for a particular time (droptime) then repeats the above;
UNTIL s2 sees the load is at the top limit, at which time it immediately switches v1 OFF immediately (important or we have a big crash), regardless of the position of the cylinder.
after a wait short wait (sleeptime, which i see wasn't included before the first steps, but will be next), v2 (second relay controlling second valve) goes ON which extends a small cyl and releases the ratchet
(the load returns to bottom and cycle starts again after waittime and second sleeptime)
« Last Edit: February 02, 2011, 05:47:07 pm by Byron Morgan » Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

s1state being high and s2state staying low tells me that my piston has reached the maximum stroke without the load reaching it's top. if s2state goes high before s1state, then the load is at the top and it's got to set v1 low immediately or my whole contraption will be torn off the wall. THat's where i'm not sure; how to tell it that s2state = high means immediate v1 = low, not waiting or caring about s1state

I'm having a little trouble following which valve makes it go up and which switch gets closed when it get there with all the scrolling up & down to read this thread.
Check your description, you may have the usages a little mixed up.

Code:
    if (s1state == HIGH)  // Bottom of stroke
    {
      digitalWrite(v1, LOW);  // Re Extend  << This indicates off due to reaching top??
      delay(droptime);
    }
    if (s2state == HIGH)  // Top of load
    {
     // probably want the write (v1, LOW) in here then.
      movingload = 0 ; // changed == to =
    }

Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Pages: [1]   Go Up
Jump to: