Go Down

Topic: dysfunctional function (Read 746 times) previous topic - next topic


Code: [Select]
  while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
    myStepper1.step(-1);  //move left
    Serial.println (leftSwitchPin);

Are you expecting the pin number to magically change in the middle of the while loop?


Thanks for the advice! Firstly, I certainly didn't intend each function to run in sequence as you describe. Rather, each should occur simultaneously. The servos, motors, and steppers all should fire at the same time while the sensor value is less than 70.  Does this mean that calling the functions within the loop is an altogether wrong method?

As for power, I have an 8 amp, 12v supply. I'll disable the functions, one by one as you suggest.

To get everything to run concurrently, you will need to implement one or more Finite State Machines.  Personally, because all the motors and things are doing such radically different actions, I'd probably create one separate FSM for each device, with the sensor triggering the start / end of them all at once.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10


Mar 18, 2013, 12:52 pm Last Edit: Mar 18, 2013, 12:54 pm by scottyjr Reason: 1
Code: [Select]
int sonarVal =0;

void setup()
 pinMode (leftSwitchPin,INPUT);               //shift to pin 2
 pinMode (rightSwitchPin,INPUT);               //shift to pin 3
pinMode(motorLeft, OUTPUT);  //dc motors
pinMode(motorMiddle, OUTPUT);
pinMode(motorRight, OUTPUT);


void loop ()
sonarVal = analogRead (sonarPin);
Serial.println (sonarVal);
if (sonarVal < 70)

Is this correct? Assigning sonarVal as an input pin (0) then using it to store a variable?


Assigning sonarVal as an input pin (0) then using it to store a variable?

That code is NOT assigning sonarVal as an input pin. sonarPin is assigned as an input pin. The value read from that pin is assigned to sonarVal.

What kind of sonar is it? Does it really return an analog value?


Is this correct? Assigning sonarVal as an input pin (0) then using it to store a variable?

No.  You are confusing yourself (and me)

A variable with a name like sonarVal would normally hold a value whilst one like sonarPin would hold a pin number.  There is nothing sacrosanct about this but it helps when understanding code.

You have set sonarVal to zero, defined it as in INPUT (why ?) then try to read from the sonarPin which has not been defined but which you have tried to set as an INPUT.

Review the pin(s) that you are using and the variable(s) that hold value(s) read from sensor(s)
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up