writing a single program which has various steps/codes?

Hey everyone!

I am currently attempting a programming question and the question requires to write a single program which does the following steps:

1.Read the value (name the variable flexValue) of a “flex sensor” that is connected to analog input AN0 of the Arduino. The value will be a 10-bit integer number having a value between 0 and 1023 inclusive, although flexing the sensor strip might not give values that cover the full range.

2.Read the value (potValue) of a potentiometer connected to analog input AN1. The value will be a 10-bit integer, similar to flexValue.

3.Output a value (dcSpeed) to a DC motor that is connected to digital pin 10 of the Arduino. This pin gives a ‘pulse-width modulated’ (PWM) output that switches rapidly between +0V and +5V to give the effect of a voltage that can vary continuously between these values. An Arduino output pin cannot provide enough current to drive the DC motor in the SIK so a transistor must be used to
increase the current.

4.Output a value (servoPosition) to a position servo that is connected to digital pin 11 of the Arduino.

5.Output the values ofthe four variables potValue, servoPosition, flexValue and dcSpeed to the serial monitor in the form of commaseparated variables (CSVs), without spaces. Each line must finish with new line character '\n', which is automatically inserted by Serial.println(). An example of a suitable print of CSVs is 815,148,846,198.

QN: I wrote the code for step 1, then saved the arduino file, did another code for step 2, saved, etc till step 5. They are all separate arduino files. The qn says to write a SINGLE program. How do i combine the individual codes to make a single program? Note: my code files for each step has its own void setup and void loop .

Thanks for the clarification! :slight_smile:

I wrote the code for step 1, then saved the arduino file, did another code for step 2, saved, etc

Why did you do that, instead of writing a single program?

To learn how to make more complex programs, study some of the many examples that come with the Arduino software package, or on line.

How about you post your code too...Then it's easier for people to help

i wrote up the codes for each steps individually, as prior to the question, there was a hint to write them up separately before moving on to the next step so that its easier to debug.

Here are my codes for each step:

STEP 1:

// assign variable flexValue value of analog iput AN0

flexValue = analogRead(A0);

// the setup routine runs once when you press reset:

void setup()
{

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

}

// the loop routine runs over and over again forever:

void loop()

{

// read the input on analog pin 0:

int flexValue = analogRead(A0);

// print out the value you read:

Serial.print(flexValue);

// delay in between reads for stability

delay(1000);

}

STEP 2:

//
#define POTPIN A1

// select the input pin for the potentiometer
// Initialise to a known value
int potValue = POTPIN;

// the setup routine runs once when you press reset:
void setup() {
pinMode( POTPIN, INPUT ); // Configure the potpin as an input

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin:
int potValue = analogRead(POTPIN);

// print out the value you read:
Serial.print(potValue);

// delay in between reads for stability
delay(1000);
}

STEP 3:

int motorPin = 10; // Connect the base of the transistor to pin 10.
// Even though it's not directly connected to the motor,
// we'll call it the 'motorPin'

int dcSpeed = motorPin;

void setup()
{
pinMode(motorPin, OUTPUT); // set up the pin as an OUTPUT
Serial.begin(9600); // initialize Serial communications
}

void loop()
{ // This basically replicates a blink, but with the motorPin instead.
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off

digitalWrite(motorPin, 255); // turn the motor on (full speed)
delay(onTime); // delay for onTime milliseconds
digitalWrite(motorPin, 0); // turn the motor off
delay(offTime); // delay for offTime milliseconds
}

STEP 4:

#include <Servo.h>
Servo servo1; // assign name to servo control object as servo1

int servoPosition = 90; /* assign value to variable seroPosition as 90, meaning the intended
angle of rotation of servo is 90 degrees, as chosen by us */

void setup()
{

servo1.attach(11); //Connect the servo to pin 11
//with a minimum pulse width of
//900 and a maximum pulse width of
//2100.
}

void loop()
{

/* To control a servo, give it the angle you'd like it
to turn to. Servos cannot turn a full 360 degrees, but
can tell it to move anywhere between 0 and 180 degrees.
Here, we are giving this value to be 90 degrees, as chosen by us*/

// Change position at full speed:

servo1.write(servoPosition); // Tell servo to go to 90 degrees
delay(1000); // delay for 1s(1000ms) for stability
servo1.write(0); // return to original position
delay(1000); // delay for 1s(1000ms) for stability

}


Just need to answer the question, which asks to write a single program which does these 5 steps, whereas here ive got codes for each step, individually. How do i write a single program with the above codes? Thats the bit that im confused about. Thanks for the help!

To learn how to make more complex programs, study some of the many examples that come with the Arduino software package, or on line.

preethi16:
. How do i combine the individual codes to make a single program? Note: my code files for each step has its own void setup and void loop .

Thanks for the clarification! :slight_smile:

Copy/paste. Everything that comes before setup in the individual programs should come before setup in the final program.
Everything between setup and loop in the individual programs should be between setup and loop in final program.
Everything after loop in individual programs should be after loop in final program. Here it is very likely you need to make adjustments. You need to adjust or replace print commands to get the result you want. If you end up with several delays to slow down serial output you can get rid of all but one.

Ohh.. i get it.. Thank You So Much !!! :slight_smile:

just clarifying:

for the qn here:

Write a single Arduino program which does the following:

1.Read the value (name the variable flexValue) of a “flex sensor” that is connected to analog input AN0 of the Arduino. The value will be a 10-bit integer number having a value between 0 and 1023 inclusive, although flexing the sensor strip might not give values that cover the full range.

2.Read the value (potValue) of a potentiometer connected to analog input AN1. The value will be a 10-bit integer, similar to flexValue.

3.Output a value (dcSpeed) to a DC motor that is connected to digital pin 10 of the Arduino. This pin gives a ‘pulse-width modulated’ (PWM) output that switches rapidly between +0V and +5V to give the effect of a voltage that can vary continuously between these values. An Arduino output pin cannot provide enough current to drive the DC motor in the SIK so a transistor must be used to
increase the current.

4.Output a value (servoPosition) to a position servo that is connected to digital pin 11 of the Arduino.

5.Output the values ofthe four variables potValue, servoPosition, flexValue and dcSpeed to the serial monitor in the form of commaseparated variables (CSVs), without spaces. Each line must finish with new line character '\n', which is automatically inserted by Serial.println(). An example of a suitable print of CSVs is 815,148,846,198.

This is the program ive got: Is this correct?

// assign variable flexValue value of analog input AN0

int flexValue = analogRead(A0);

// the setup routine runs once when you press reset:

#define POTPIN A1

// select the input pin for the potentiometer
// Initialise to a known value

int potValue = POTPIN;

// the setup routine runs once when you press reset:

int motorPin = 10; // Connect the base of the transistor to pin 10.
// Even though it's not directly connected to the motor,
// we'll call it the 'motorPin'

int dcSpeed = motorPin;

#include <Servo.h>

// assign name to servo control object as servo1

Servo servo1;

int servoPosition = 90; /* assign value to variable seroPosition as 90, meaning the intended
angle of rotation of servo is 90 degrees, as chosen by us */

void setup()
{

pinMode( POTPIN, INPUT ); // Configure the potpin as an input

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

servo1.attach(11); //Connect the servo to pin 11
//with a minimum pulse width of
//900 and a maximum pulse width of
//2100.

}

// the loop routine runs over and over again forever:

void loop()

{

// read the input on analog pin 0:

int flexValue = analogRead(A0);

// print out the value you read as 'flexValue'
Serial.printIn("flexValue");
Serial.print(flexValue);

// delay in between reads for stability

delay(1000);

// read the input on analog pin:
int potValue = analogRead(POTPIN);

// print out the value you read, as 'potValue'
Serial.printIn("potValue");
Serial.print(potValue);

// delay in between reads for stability
delay(1000);

// Next, replicate a blink, but with the motorPin instead.
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off

digitalWrite(motorPin, 255); // turn the motor on (full speed)
delay(onTime); // delay for onTime milliseconds
digitalWrite(motorPin, 0); // turn the motor off
delay(offTime); // delay for offTime milliseconds

/* To control a servo, give it the angle you'd like it
to turn to. Servos cannot turn a full 360 degrees, but
can tell it to move anywhere between 0 and 180 degrees.
Here, we are giving this value to be 90 degrees, as chosen by us*/

// Change position at full speed:

servo1.write(servoPosition); // Tell servo to go to 90 degrees
delay(1000); // delay for 1s(1000ms) for stability
servo1.write(0); // return to original position
delay(1000); // delay for 1s(1000ms) for stability

}

//Print the values of flexValue, potValue, dcSpeed and servoPosition onto the serial monitor, in the form of CSV
serial.PrintIn("flexValue");
serial.PrintIn(flexValue);
serial.printIn("potValue");
serial.PrintIn(potValue);
serial.PrintIn("dcSpeed");
serial.Print(dcSpeed);
serial.PrintIn("servoPosition");
serial.PrintIn(servoPosition);

See:Merging Code

.

Maybe also have a look at Planning and Implementing a Program

...R