Pages: [1]   Go Down
Author Topic: i'm so bad at logic  (Read 244 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm trying to program an arduino to basically lift an object up with a motor and pulley (and some relays to control motor polarity), when the sensorTop variable gets activated, wait a few seconds, and lower the object down. when the sensorBottom variable gets activated, wait a few seconds, turn on some lights, move a second motor, and then raise the object up. again and again and again. i just can't seem to wrap my head around the logic. here's what i have so far. Any help is much appreciated!!!

Code:
//12 . 11 - pos neg
// 10 . 9 - neg pos

int sensorBottom, sensorTop;
boolean switchTop = false;
boolean switchBottom = false;

void setup() {

  pinMode(12, OUTPUT); //1st motor
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT); //2nd motor
  pinMode(7,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
  sensorBottom = analogRead(1);
  sensorTop = analogRead(0);

}

void loop() {

  //start by picking up??
  //digitalWrite(12, HIGH);
  //digitalWrite(11, HIGH);
  //digitalWrite(10, LOW);
 // digitalWrite(9, LOW);

  //get stuck in infinite if loops//

  //if top sensor is activated wait for 4 sec and then let down
  if ((sensorTop > 500)&&(switchTop == false)){
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    delay(4000);
    //flip switch
    switchBottom=false;
    switchTop=true;

  }
  if (switchTop == true) {
    //lower
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);

  }


  if ((sensorBottom > 500)&&(switchBottom == false)) {
    delay(2000);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(4, LOW);
    //2) turn on all lights and spin motor
    delay(200);
    digitalWrite(8, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    delay(3000);
    //3)turn everything off and start over again
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    switchBottom=true;
    switchTop=false;
  }

  if (switchBottom == true) {   
    //pick UP
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
  }

}
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that you read the top and bottom sensors only in setup(), but that you use those same readings as the basis for control in loop(). Do these values need to be updated?
Logged

There's always a better way!

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would have most of your code in the setup() as it only runs once.

Maybe something like this:
Code:
void setup(){
  while(topsensor == FASLE){
  //run motor to lift object
  }
  delay(a few seconds);
  while(bottomsensor == FALSE){
    //run motor to descend object
  }

  delay(a few seconds);

  //turn on some lights

  //run motor 2
}

void loop(){
  //raise object again and again
}
Logged


UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to make a fundamental decision: whether you are going to go through the complete cycle within one execution of the loop() function, or execute loop() repeatedly and keep track of what is happening and whether it is time to do anything each time through.

The first approach is going to give you simpler code, but means that you can really only do one thing at a time - if you want to do anything else independently of the up/down cycle, it would be very difficult to add. The second approach means more complexity (but not a LOT more) but means you could do as many other things at the same time as you wanted.

Any preference which approach to take?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i might want to do other things so I have the code in loop. i just had to move the sensor readings down into loop. i don't know what i was thinking! it's working okay now. thanks so much for the help!
Logged

Pages: [1]   Go Up
Jump to: