Epilog Challenge 9

Sew Warm Contest 2018

Gluten Free Challenge

Tips

Questions

Comments

From a wiring point of view - it looks good. You got everything connected correctly. Without building it - I can't be 100% certain but it does look like you covered your bases.

From the code point of view - I am having trouble following you. (I did see that the #include <Servo.h> was not right at the top - minor oversight when you moved the code to the web site). I am following the logic and see the following where you get the initial vol from the IR Sensor:

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

Makes since.

if vol >.7, you rotate the servo. (0, 90, 180, and back to 90). You don't resample the voltage so vol will still be the value from the last reading. Next, as you go through all the different positions of the servo, you set the pos = that degree. at the end of the servo, you set it back to 90. So the big section of if statements only match on the POS ==90, and vol >.7. So only F ever gets set..

Try this code (I did not test it - didn't have all the hardware to build for testing but it should work better);

#include <Servo.h>

Servo MS;

int a = 4;

int b = 5;

int c = 6;

int d = 7;

int pos = 0;

//declare these 3 variable as global so they don't have to be passed in and out of the subroutine

int S = 0; //should always set a value when you declare a variable

int F = 0;

int D = 0;

void setup()

{

Serial.begin(9600);

pinMode(a,OUTPUT);

pinMode(b,OUTPUT);

pinMode(c,OUTPUT);

pinMode(d,OUTPUT);

MS.attach(3);

}

void loop()

{

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

if(vol >= 0.7)

{

MS.write(0);

pos = 0;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(90);

pos = 90;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(180);

pos = 180;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(90);

pos = 90;

delay(500);

Serial.println(pos);

}

}

void which_way(int pos)

{

//pos is declared inside the subrouting and is independ of the other pos

//remeasure distance at the new servo position

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

if(pos == 0 && vol >= 0.7)

{

S = 1;

}

if(pos == 0 && vol <= 0.7)

{

S = 0;

}

if(pos == 90 && vol >= 0.7)

{

F = 1;

}

if(pos == 90 && vol <= 0.7)

{

F = 0;

}

if(pos == 180 && vol >= 0.7)

{

D = 1;

}

if(pos == 180 && vol <= 0.7)

{

D = 0;

}

if(S == 0 && F == 1 && D == 1)

{

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

}

if(S == 1 && F == 0 && D == 1)

{

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

}

if(S == 1 && F == 1 && D == 0)

{

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

}

if(S == 1 && F == 1 && D == 1)

{

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

}

Serial.println(S);

Serial.println(F);

Serial.println(D);

}

Newsletter

Let your inbox help you discover our best projects, classes, and contests. Instructables will help you learn how to make anything!