knowt logo

APSC 160 Final

All Second-half Material


Arduino Functions

Digital I/O

  • digitalRead()

  • digitalWrite()

  • pinMode()

digitalRead();

  • reads the value from a specified digital pin, either HIGH or LOW

Syntax

digitalRead(pin);

Parameters

  • pin: the Arduino pin number you want to read

Returns

  • HIGH or LOW


digitalWrite();

  • write a HIGH or LOW value to a digital pin

Syntax

digitalWrite(pin, value);

Parameters

  • pin: the Arduino pin number

  • value: HIGH or LOW

Returns

  • nothing


pinMode()

  • configures the specified pin to behave as an input or an output

Syntax

pinMode(pin, mode);

Parameters

  • pin: the Arduino pin number to set the mode of

  • mode: INPUT or OUTPUT

Returns

  • nothing


Example Code with Digital I/O

int ledPin = 13;
int inPin = 7;
int val = 0;

void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

Analog I/O

For analog, values may vary. For example with sensors, signals are analog(continuous values) rather than discrete like a switch.

  • analogRead()

  • analogReference()

  • analogWrite()


analogRead();

  • reads the value from the specific analog pin

  • uses pins A0-A5

Syntax

analogRead(pin);

Parameters

  • pin: the corresponding analog pin we want to read from

Returns

  • integer value between 0 and 1023 (10-bit binary value)


Example Code with Analog and Digital I/O

int sensorValue = 0;

#define LED 13
#define POT A0 // for potentimeter

void setup ()
{
pinMode(POT, INPUT);
pinMode(LED, OUTPUT);
}

void loop ()
{
int sensorValue = analogRead(POT);
digitalWrite(LED, HIGH);
delay(sensorValue);
digitalWrite(LED, OFF)
}

Reading a Potentiometer

V = k/1024 × 5V (or however many volts you are using.

k is the read value from the potentiometer

Others

random();

  • generate pseudo-random numbers

Syntax

random(max);
random(min, max);

Parameters

  • min: lower bound of the random value, inclusive (optional)

  • max: upper bound of the random value, exclusive.

Returns:

  • a random number between min and max-1. Data type: long


randomSeed();

  • generates a random number starting at a point in its random sequence. While the sequence may be very long, it is always the same.

  • only used one during setup, random() will then refer back to it

Syntax

randomSeed(seed)

Parameters

  • seed: non-zero number to initialize the pseudo-random sequence. Allowed data types: unsigned long.

Returns:

  • nothing


Serial.println();

Syntax

Serial.println(val);
Serial.println("word");

Parameters:

  • val: the value to print. can be any data type

  • format

**There is also Serial.print()… works in the same way but only print the number or string. Whereas, Serial.println() prints it with a newline character!


Serial.begin()

Syntax

Serial.begin(speed);

Parameters

  • Speed: bits per second. Data Type: long.

  • Typically 9600

Time

millis();

  • returns the number of milliseconds passed since the Arduino board began running the current program

Syntax

time = millis()

Parameters

  • none

Returns

  • number of milliseconds passed since the program started. Data type: unsigned long.

Example Code: Prints the number of milliseconds passed since the Arduino board started running the code.

unsigned long myTime;

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time: ");
myTime = millis();

Serial.println(myTime); // prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}

Practice Example: Runners Tool - Advanced Timing

delay();

Strings

Strings

  • character array (array of type char) in which the last array element is the null character ‘\0’.

  • you can find the size of the array knowing the last element if the array is ‘\0’

String Constant

  • eg: “sensor.txt”

  • text between double quotes

  • last element is still ‘\0’

Syntax

char filename[] = {'t', 'e', 's', 't', '\0'};
char filenameA[] = "test";

Printing Strings

char label[] = "size";
printf("%s \n", label);

//printing with spacing
printf("%10s \n",label);

Scanning Strings

#define LINEMAX 100
char line[LINEMAX];

//ex 1
scanf("%s", line);

//ex 2. if you want to scan a certain amount of characters only...
scanf("%4s", line);
  • Notice how you do not need an ‘&’

  • If the input is two words, the scan will only pick up the first

  • Example 2: If you specify how many characters you want to read (for example 4), then it will only read the first four inputted characters. If you type in “something“, it will only scan “some”


Creating a function that returns the size of a string

char line[] = "test";

int stringLength(char s[]);


int stringLength(char s[]){

int count = 0;

for (int index = 0; line[index] != '/0'; index++)
{
count++;
}

return count;
}

string.h Library


strlen(s)

  • returns the length of string s

strcpy(s, t)

  • copies string t to string s


sprintf();

  • creating a new string from the values contained in one or more variables

Syntax

sprintf(destSpring, formatString, var1...varN);

Example

int num;
char arrayChar[100];

printf("Enter a number: ");
scanf("%d", &num);

sprintf(arrayChar, "myfile - %i", num);
printf("file name is %s\n", fileName);

//Given the input is 30, output should be: file name is myfile - 30

sscanf();

  • scans each word/char separated by a space inside the string into new variables

Syntax

sscanf(char str, char format, va1... varN)

Example

int main () {
int day, year;
char weekday[20], month[20], dtm[100];

strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );

printf("%s %d, %d = %s\n", month, day, year, weekday );

return(0);
}

strcat();

  • concatenates(merges) the destination string and the source string

Syntax

strcat(destinationString, sourceString);

Example

int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);

puts(str1);
puts(str2);

return 0;
}

The output will be:

This is programiz.com
programiz.com

The size of the destination string NEEDS to be large enough to store the resultant string

APSC 160 Final

All Second-half Material


Arduino Functions

Digital I/O

  • digitalRead()

  • digitalWrite()

  • pinMode()

digitalRead();

  • reads the value from a specified digital pin, either HIGH or LOW

Syntax

digitalRead(pin);

Parameters

  • pin: the Arduino pin number you want to read

Returns

  • HIGH or LOW


digitalWrite();

  • write a HIGH or LOW value to a digital pin

Syntax

digitalWrite(pin, value);

Parameters

  • pin: the Arduino pin number

  • value: HIGH or LOW

Returns

  • nothing


pinMode()

  • configures the specified pin to behave as an input or an output

Syntax

pinMode(pin, mode);

Parameters

  • pin: the Arduino pin number to set the mode of

  • mode: INPUT or OUTPUT

Returns

  • nothing


Example Code with Digital I/O

int ledPin = 13;
int inPin = 7;
int val = 0;

void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

Analog I/O

For analog, values may vary. For example with sensors, signals are analog(continuous values) rather than discrete like a switch.

  • analogRead()

  • analogReference()

  • analogWrite()


analogRead();

  • reads the value from the specific analog pin

  • uses pins A0-A5

Syntax

analogRead(pin);

Parameters

  • pin: the corresponding analog pin we want to read from

Returns

  • integer value between 0 and 1023 (10-bit binary value)


Example Code with Analog and Digital I/O

int sensorValue = 0;

#define LED 13
#define POT A0 // for potentimeter

void setup ()
{
pinMode(POT, INPUT);
pinMode(LED, OUTPUT);
}

void loop ()
{
int sensorValue = analogRead(POT);
digitalWrite(LED, HIGH);
delay(sensorValue);
digitalWrite(LED, OFF)
}

Reading a Potentiometer

V = k/1024 × 5V (or however many volts you are using.

k is the read value from the potentiometer

Others

random();

  • generate pseudo-random numbers

Syntax

random(max);
random(min, max);

Parameters

  • min: lower bound of the random value, inclusive (optional)

  • max: upper bound of the random value, exclusive.

Returns:

  • a random number between min and max-1. Data type: long


randomSeed();

  • generates a random number starting at a point in its random sequence. While the sequence may be very long, it is always the same.

  • only used one during setup, random() will then refer back to it

Syntax

randomSeed(seed)

Parameters

  • seed: non-zero number to initialize the pseudo-random sequence. Allowed data types: unsigned long.

Returns:

  • nothing


Serial.println();

Syntax

Serial.println(val);
Serial.println("word");

Parameters:

  • val: the value to print. can be any data type

  • format

**There is also Serial.print()… works in the same way but only print the number or string. Whereas, Serial.println() prints it with a newline character!


Serial.begin()

Syntax

Serial.begin(speed);

Parameters

  • Speed: bits per second. Data Type: long.

  • Typically 9600

Time

millis();

  • returns the number of milliseconds passed since the Arduino board began running the current program

Syntax

time = millis()

Parameters

  • none

Returns

  • number of milliseconds passed since the program started. Data type: unsigned long.

Example Code: Prints the number of milliseconds passed since the Arduino board started running the code.

unsigned long myTime;

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time: ");
myTime = millis();

Serial.println(myTime); // prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}

Practice Example: Runners Tool - Advanced Timing

delay();

Strings

Strings

  • character array (array of type char) in which the last array element is the null character ‘\0’.

  • you can find the size of the array knowing the last element if the array is ‘\0’

String Constant

  • eg: “sensor.txt”

  • text between double quotes

  • last element is still ‘\0’

Syntax

char filename[] = {'t', 'e', 's', 't', '\0'};
char filenameA[] = "test";

Printing Strings

char label[] = "size";
printf("%s \n", label);

//printing with spacing
printf("%10s \n",label);

Scanning Strings

#define LINEMAX 100
char line[LINEMAX];

//ex 1
scanf("%s", line);

//ex 2. if you want to scan a certain amount of characters only...
scanf("%4s", line);
  • Notice how you do not need an ‘&’

  • If the input is two words, the scan will only pick up the first

  • Example 2: If you specify how many characters you want to read (for example 4), then it will only read the first four inputted characters. If you type in “something“, it will only scan “some”


Creating a function that returns the size of a string

char line[] = "test";

int stringLength(char s[]);


int stringLength(char s[]){

int count = 0;

for (int index = 0; line[index] != '/0'; index++)
{
count++;
}

return count;
}

string.h Library


strlen(s)

  • returns the length of string s

strcpy(s, t)

  • copies string t to string s


sprintf();

  • creating a new string from the values contained in one or more variables

Syntax

sprintf(destSpring, formatString, var1...varN);

Example

int num;
char arrayChar[100];

printf("Enter a number: ");
scanf("%d", &num);

sprintf(arrayChar, "myfile - %i", num);
printf("file name is %s\n", fileName);

//Given the input is 30, output should be: file name is myfile - 30

sscanf();

  • scans each word/char separated by a space inside the string into new variables

Syntax

sscanf(char str, char format, va1... varN)

Example

int main () {
int day, year;
char weekday[20], month[20], dtm[100];

strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );

printf("%s %d, %d = %s\n", month, day, year, weekday );

return(0);
}

strcat();

  • concatenates(merges) the destination string and the source string

Syntax

strcat(destinationString, sourceString);

Example

int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);

puts(str1);
puts(str2);

return 0;
}

The output will be:

This is programiz.com
programiz.com

The size of the destination string NEEDS to be large enough to store the resultant string

robot