Go Down

Topic: Would someone be willing to proofread my code? Thanks!! (Read 680 times) previous topic - next topic

johnnydolly22

Quote

// linear actuator brown wire to the right
#include <Servo.h>
 
Servo spoon_servo;
Servo bowl_servo;
int pos = 35; // initialize spoon to serving position

void setup(){
  pinMode(4,OUTPUT); // pin output, spoon extend
  pinMode(8,OUTPUT); // pin output, spoon retract
  pinMode(11,INPUT); // linear actuator limit switch, front
  pinMode(3,INPUT); // linear actuator limit switch, back
  pinMode(12,INPUT); // user input switch, not sure if this line is needed
  pinMode(6,INPUT); // home position rotary switch
  spoon_servo.attach(9);
  spoon_servo.write(pos);
  delay(2000);
  bowl_servo.attach(10);
  bowl_servo.write(90);
  if (digitalRead(6) == HIGH){
      while (digitalRead(11) == LOW) { // fully retract spoon
  digitalWrite(8, HIGH);
  }
  digitalWrite(8, LOW); // stop retracting spoon
  }
  else{      while (digitalRead(3) == LOW) { // fully retract spoon
  digitalWrite(4, HIGH);
  }
  digitalWrite(4, LOW); // stop retracting spoon
  }

}

void loop(){
  if(digitalRead(12) == HIGH) { 
       while (digitalRead(11) == LOW) {
       digitalWrite(8, HIGH); // fully retract spoon
       }
       digitalWrite(8, LOW); // stop retracting spoon   
       delay(1000);     
       spoon_servo.write(120); // rotate spoon into bowl
       delay(1000);
       bowl_servo.write(110); // rotate bowl
       delay(1000);
       bowl_servo.write(90); // stop rotation of bowl
       delay(1000);
       spoon_servo.write(35); // rotate spoon into serving position
       delay(1000);
       while (digitalRead(3) == LOW) {
       digitalWrite(4, HIGH); // fully extend spoon
       }
       digitalWrite(4, LOW); // stop extending spoon       
  }
  else{}
return
}


Korman

Does it compile?
Does it do what you want it to do?
Does any part of your code look problematic to you?

If none of the above, be happy and move along. Some of you code might be improved, but if it does what you want, be happy and leave it as it is.

Korman

PaulS

Code: [Select]
  if (digitalRead(6) == HIGH){
      while (digitalRead(11) == LOW) { // fully retract spoon
  digitalWrite(8, HIGH);
  }
  digitalWrite(8, LOW); // stop retracting spoon
  }
  else{      while (digitalRead(3) == LOW) { // fully retract spoon
  digitalWrite(4, HIGH);
  }
  digitalWrite(4, LOW); // stop retracting spoon
  }

Putting this code in setup means that you have to be holding the switch connected to pin 6 (which should be named using a #define statement, in my opinion) down when you reset the Arduino in order for the code to be executed. This seems unlikely, so this code is not likely to ever be executed.

Is pin 3 the spoon retract pin, or is pin 11?

The indenting is lousy. Put each { on a new line, and indent the code between the aligned { and } one tab space. This makes the code much easier to read, and to see the block structure.

Code: [Select]
       digitalWrite(8, HIGH); // fully retract spoon
OK. Which damn pin is the spoon retractor attached to? You've now identified 3 pins for that purpose.

Code: [Select]
  else{}
Well, this is useful. The else clause should be omitted when there is nothing to do.

gardner

Your indentation scheme is a little odd.  I found it easier to understand the logic by pasting it into the Arduino IDE and using the Ctrl-T auto-format function.  If you want others to try to look at your code, best practice is to clean up the indentation with Ctrl-T before posting.

Go Up