2 Arti Instruksi Constrain, Millis , Map ,min Dan Max Rev1jul17.docx

  • Uploaded by: Charles Robiansyah
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 2 Arti Instruksi Constrain, Millis , Map ,min Dan Max Rev1jul17.docx as PDF for free.

More details

  • Words: 703
  • Pages: 4
constrain(x, a, b) Description Constrains a number to be within a range. Parameters x: the number to constrain, all data types a: the lower end of the range, all data types b: the upper end of the range, all data types Returns x: if x is between a and b a: if x is less than a b: if x is greater than b Example sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150

millis() https://www.arduino.cc/en/reference/millis Description 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. Parameters None Returns Number of milliseconds since the program started (unsigned long) Example unsigned long time; void setup(){ Serial.begin(9600); } void loop(){ Serial.print("Time: "); time = millis(); 1

//prints time since program started Serial.println(time); // wait a second so as not to send massive amounts of data delay(1000); } Tip: Note that the parameter for millis is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints.

MAP: In order to scale this value, use a function called map() outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high range of the raw data, and the low and high values for that data to be scaled too. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

map(value, fromLow, fromHigh, toLow, toHigh) Description Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc. Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired. Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example y = map(x, 1, 50, 50, 1); The function also handles negative numbers well, so that this example y = map(x, 1, 50, 50, -100); is also valid and works well. The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

2

Parameters value: the number to map fromLow: the lower bound of the value's current range fromHigh: the upper bound of the value's current range toLow: the lower bound of the value's target range toHigh: the upper bound of the value's target range

Returns The mapped value. Example /* Map an analog value to 8 bits (0 to 255) */ void setup() {} void loop() { int val = analogRead(0); val = map(val, 0, 1023, 0, 255); analogWrite(9, val); } https://www.arduino.cc/en/Reference/Map

min(x, y) Description Calculates the minimum of two numbers. Parameters x: the first number, any data type y: the second number, any data type Returns The smaller of the two numbers. Examples

sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100 // ensuring that it never gets above 100. 3

Note Perhaps counter-intuitively, max() is often used to constrain the lower end of a variable's range, while min() is used to constrain the upper end of the range. Warning Because of the way the min() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results min(a++, 100); // avoid this - yields incorrect results min(a, 100); a++;

// use this instead - keep other math outside the function

max(x, y) Description Calculates the maximum of two numbers. Parameters x: the first number, any data type y: the second number, any data type Returns The larger of the two parameter values. Example

sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20 // (effectively ensuring that it is at least 20)

4

Related Documents


More Documents from ""