Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I would like to know if there was anyway to combine two codes so that for

ID: 3712967 • Letter: H

Question

Hello,

I would like to know if there was anyway to combine two codes so that for the first two minutes that the arduino is turned on, the first sketch runs and then after the two minutes are up, the second sketch runs. I need the codes to run separately.

Here is the first sketch:

#include <Servo.h> //accesses the Arduino Servo Library

Servo myservo; // creates servo object to control a servo

int val;    // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // ensures output to servo on pin 9
}

void loop()
{

val = analogRead(1);            // reads the value of the potentiometer from A1 (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180);     // converts reading from potentiometer to an output value in degrees of rotation that the servo can understand
myservo.write(val);                  // sets the servo position according to the input from the potentiometer
delay(15);                           // waits 15ms for the servo to get to set position
}

Here is the second sketch:

#include <Servo.h> //accesses the Arduino Servo Library

Servo myservo; // creates servo object to control a servo

int val;    // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // ensures output to servo on pin 9
}


void loop()
{
val = analogRead(1);            // reads the value of the potentiometer from A1 (value between 0 and 1023)
//use this code for setting deadZone
float volt = val*(5.0/1023.0);
if(volt < 2.5){
    val = 0;
}
val = map(val, 0, 1023, 0, 180);     // converts reading from potentiometer to an output value in degrees of rotation that the servo can understand
myservo.write(val);                  // sets the servo position according to the input from the potentiometer
delay(15);                           // waits 15ms for the servo to get to set position
}

Explanation / Answer

Yeah ! you can actually do what you want to. But burning two sketches simultaneously on the arduino boards is not possible.

You can implement what you want to in one sketch.

you can use the millis() function of Arduino. It Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

the implementation can be like this:

unsigned long end_time;

Servo myservo;

int val;

void setup()

{

myservo.attach(9);

end_time = millis() + 120000; // 60seconds * 2 minutes * 1000ms

}

void loop(){

val = analogRead(1);

if(millis() > end_time){

float volt = val*(5.0/1023.0);

if(volt<2.5){

val = 0;

}

}

val = map(val,0,1023,0,180);

myservo.write(val);

dealy(15);

}

the if condition

if(millis() > end_time)

will be true after 2 minutes of up time.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote