Ardunio Basic Help

Arduino Help -- Some Basic Arduino Knowledge

Insulate the bottom of your Arduino. All those rough pins on the bottom can get shorted out by anything metallic. Tape on a rectangle of cereal-box cardboard.

You can destroy your Arduino by static-shocking it, during dry weather. Just before you touch your Arduino, touch the metal USB connector on the left edge of the Arduino to discharge your static. (Static comes from moving your feet across the floor or sitting down on a chair or rising from a chair.) Or you can touch a metal power supply case or any other metal that is ground.

A special way to destroy your Arduino: merely hand it to someone else. If it is a dry day, there will be static that will jump through the Arduino into the other person. Do one of these. 1) Set Arduino on a table and let the other person touch the USB connector, then pick it up. 2) Make wrist-to-wrist contact before handing off.

Which Arduino to purchase? Old ones: Duemilanove, Diecimila

New ones: Uno revision 3, Mega 2560, Due Uno is cheaper but limited pins. Mega 2560 and Due have 54 digital pins. Due has a fast ARM microcontroller but is limited to 3.3V signals. Due needs a micro-B USB cable whereas Uno and Mega 2560 need the old, large USB B cable.

Where is it made? The best ones are made in Italy. The cheap ones from China are 60% cheaper but may not be labeled as well and may not respond to Tools>Board Info.

Your C program is what Arduino calls a sketch. If you are new to Arduino, learn by looking at examples, like at the end of this document. https://www.arduino.cc/en/Tutorial

Before getting into the C language, we need to check out logic and variables.

There is also the char type, check that out by searching Internet.

If you need to write a value to a digital pin, use a boolean variable or a logical expression or HIGH or LOW.

digitalWrite(pinNumberA, HIGH);

digitalWrite(pinNumB, LEDcondition);

digitalWrite(pinNum3, a>=d3); //a and d3 are variables

digitalWrite(redLEDpin, limitVal == d3); //there is a difference between = and ==

Use // to make a comment, such as the two lines above.

/* Or you can make a running comment

across several lines

by enclosing in slash asterisk also called slash splat */

To measure a pin, use a voltmeter. HIGH is 3.3 volt or 5 volt, LOW is 0 volt but only if the pin has been made an output in setup(), pinMode(LEDpin,OUTPUT);

Examples of a binary count-up, 2-bit and 4-bit

00 01 10 11

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

AND && OR ||

NOT ! example !moveItUp !true is the same as false !false is the same as true

Combinations are OK: ( !inhibit && rotate && validA) || overrideSwitch

Examples of Logic in English and C

left Twix OR right Twix

no left Twix OR right Twix

no left Twix OR no right Twix

eat

eat

!eat not eat

lamp plugged in AND bulb screwed in AND switch on

lamp plugged in AND no bulb screwed in AND switch on

lamp not plugged in AND bulb screwed in AND switch not on

light

!light

!light

At the airport terminal

Would you use AND or OR for streaming, goToTheater, redBox, DVDatLibrary ?

Would you use AND or OR for watermelon, knife ?

Subtleties

These are used in an if statement < > <= >= ==

A boolean variable may be used alone if (moveItUp) { i++; } Pronounce it "if moveItUp is true, increment i."

This is the same as if (moveItUp == true) { i++; }

Boolean variables may be Not'ed if (!moveItDown) { i++; }

Pronounce it "if moveItDown is false, increment i."

Algebra

C language

C language

y = mx + b

y = m*x + b; if you code mx+b, C thinks you are adding two variables, mx and b

y = mx + b; /* mx is considered to be one variable, not a multiplication. y will be updated with the sum of mx and b. If mx has not been declared to be a variable, there will be a compiler message. */

C language

numOfStudents = 8 ;

numOfStudents-- ;

numOfStudents-- ;

numOfStudents-- ;

Serial.println(numOfStudents) ;

numOfStudents = numOfStudents - 3 ;

setup

one student leaves

another student leaves

another student leaves

it prints 5 in the serial monitor

three students leave at the same time

The last line, numOfStudents = numOfStudents - 3, is invalid in algebra, but in coding it means update the number of students.

C language

moneyInPocket = 17.26;

twix=1.69;

gum=.59;

moneyInPocket = moneyInPocket - 3*twix - 2*gum;

The last line updates the money in your pocket. How many Twix and packages of gum have been purchased?

C language

moneyInPocket = 17.26;

moneyInPocket = -moneyInPocket;

The last line is not wrong, it sometimes is useful in C, though I can't think of why. But I have seen it in an example. In algebra, the only time that x = -x is when x = 0, as follows.

These are examples that computer code and algebra are a little different.

Example

void setup() {

pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {

digitalWrite(LED_BUILTIN, HIGH);

delay(1000); //delay 1000 milliseconds which is one second

digitalWrite(LED_BUILTIN, LOW);

delay(1000);

}

Example

void setup() {

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

}

void loop() {

int sensorValue = analogRead(A0);

float voltage = sensorValue * (5.0 / 1023.0);

Serial.println(voltage);

}

Example of For

int timer = 100;

void setup() {

for (int thisPin = 2; thisPin < 8; thisPin++) {

pinMode(thisPin, OUTPUT);

}

} //makes pins 2 to 7 OUTPUTs

void loop() {

for (int thisPin = 2; thisPin < 8; thisPin++) {

digitalWrite(thisPin, HIGH);

delay(timer);

digitalWrite(thisPin, LOW);

}

for (int thisPin = 7; thisPin >= 2; thisPin--) {

digitalWrite(thisPin, HIGH);

delay(timer);

digitalWrite(thisPin, LOW);

}

}

Example of If

const int analogPin = A0;

const int ledPin = 13;

const int threshold = 400;

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(9600);

}

void loop() {

int analogValue = analogRead(analogPin);

if (analogValue > threshold) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

}

Serial.println(analogValue);

delay(1);

}

John Engelbrecht South Austin Electrical Engineer (not state licensed) July 31 2017

file name Arduino help protection logic equals.odt on GETTER