1

I'm super new to the c++ programming world (currently taking the class now) and I'm in need of some assistance with some coding that's way beyond my knowledge at the moment. I have to write a code for Arduino interfacing with servo motor, ezButton, water-proof sensor and ada fruit motor shield.

the project entails building a moveable arm that holds a hot metal post that needs to be submerged into water for cleaning, BUT before it can be submerged I need to give the metal time to cool so it doesn't fracture. (Question 1, how do I delay the servo motor from moving to allow the metal to cool.) After the metal is submerged for cleaning, the servo motor needs to move back to the starting position ONLY if the temperature is less than 50 degrees Celsius. (Question 2, how do I make the servo motor dependent on temperature.)


#include <OneWire.h>
#include <DallasTemperature.h>
#include <AFMotor.h>
#include <Servo.h>    
#include <ezButton.h>

#define ONE_WIRE_BUS 2 
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1

OneWire oneWire (ONE_WIRE_BUS); 
DallasTemperature sensors (&oneWire);

ezButton button(7);  
int loopState = LOOP_STATE_STOPPED;

Servo name_servo;    
int servo_position = 0; 
int Celsius = 0; 

void setup() {
  sensors.begin();
  Serial.begin(9600);
  name_servo.attach (10);  

  button.setDebounceTime(50);
 }

void loop() {
  button.loop();
  
if (button.isPressed()) {
    if (loopState == LOOP_STATE_STOPPED)
      loopState = LOOP_STATE_STARTED;
    else // if(loopState == LOOP_STATE_STARTED)
      loopState = LOOP_STATE_STOPPED;
  }

if (loopState == LOOP_STATE_STARTED) {
    sensors.requestTemperatures();
    Celsius = sensors.getTempCByIndex(0);
    Serial.print(Celsius);
    Serial.print(" C  ");
    delay(1000);

//ADD COOLING PERIOD (3min)

  for (servo_position = 0; servo_position <=180; servo_position +=1){
      name_servo.write(servo_position);
      delay(10);
      ;
   }

// ADD CONDITIONAL STATEMENT (if temp < 50, motor returns from 180 to 0)

  for (servo_position=180; servo_position >= 0; servo_position -=1){
      name_servo.write(servo_position);
      delay(10);
      
    }
  }
}

4
  • how do I delay the servo motor from moving to allow the metal to cool ... press the button when the metal is cool
    – jsotola
    Oct 3 at 23:43
  • @jsotola yes that true but, The project is supposed to be a push button operation that you can walk away from after loading in the metal post. I guess the key is efficiently, the operator can go finish/do other tasks while this program is running Oct 4 at 0:06
  • 1
    do this with pencil and paper ... write down in great detail how the program should behave ... if the description is very detailed, then it will translate into code almost directly
    – jsotola
    Oct 4 at 0:10
  • 1
    write a simple state machine ... use flags to interface the button and the temperature sensor to the servo control code ... all code would be in loop() ... do something like this read remperature ... if below 50 then dipDone = true ... if below 100 then okToDip = true .... read button ... if pressed then doDip = true ....... next comes the logic that controls the servo ... if doDip and okToDip and notDippingAlready then start servo ... etc. etc.
    – jsotola
    Oct 4 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.