Meano_Arduino

Power via usb or power supply.

center_positive power supply 7-12V w/min 250mA (preferably 1000mA if powering lots of stuff)
or DC 8-12V w/5.5mm/2.1mm power plug  +crimp terminals for ur battery.
  (and hopefully an inline fuse holder)
    Note: positive goes to center!!!

fyi: a basic arduino board draws ~50ma for itself

Useful string features

stringThree = stringOne + 123;   // add int    to stringstringThree = stringOne + 'A';   // add char   to string:stringThree = stringOne + "abc"; // add string to string:stringThree = stringOne + stringTwo; // add 2 Strings

Concat/merge arrays


char myBigArray[128];  myBigArray[0] = '\0';  strcat(myBigArray, myLittleArrayOne);  strcat(myBigArray, myLittleArrayTwo);  strcat(myBigArray, myLittleArrayThree);const int ledPin =  13;  

timer/millis example

int ledState = LOW;      
long previousMillis = 0; //last time LED was updated
long interval = 1000; // LED blink interval (long cuz milliseconds too big for int)
void setup() {   
  pinMode(ledPin, OUTPUT);      
}
void loop(){   
  unsigned long currentMillis = millis();   
  if(currentMillis - previousMillis > interval) {     
     previousMillis = currentMillis; //save last LED_blink time      
     if (ledState == LOW)  
         ledState = HIGH;     
     else       
         ledState = LOW;     
     digitalWrite(ledPin, ledState);   
    }
}

// Continuous rotation servos with Arduino

//Parallax Continuous Rotation Servo (#900-00008)
//Note: ensure you have a common ground (ext power ground also connected to arduino ground)
Servo servo1;
void setup(){
  servo1.attach(13);  //servo to pin 13
}
void loop(){
  servo1.write(90);  //90=neutral, 180=fast_right  0=fast_left
}

To include/install new/custom libraries (hundreds exist online)

 click sketch menu -> Manage Libraries
or
 click sketch menu -> Add zip file Library
or
 manually unzip cpp's and h files into folder, put folder into your "libraries" dir
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.cpp
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.h
  My Documents\Arduino\libraries\ArduinoParty\examples


Blink 13 built-in led (included with UNO and NANO)

https://www.youtube.com/watch?v=9SusjXjLCSw

Uno Board

http://www.arduino.cc/en/main/arduinoBoardUno

Nano $10

http://www.amazon.com/Improved-Version-Atmega328-Board-Arduino/dp/B00U8Z065G/ref=sr_1_12?ie=UTF8&qid=1429659504&sr=8-12&keywords=arduino+nano+30

Java

http://playground.arduino.cc/interfacing/java
you will need to add the RXTXcomm.jar to your class path, and you will need to add the associated JNI interface DLL (rxtxSerial.dll) to your PATH
http://www.ardulink.org/
setlocal
set PATH=%PATH%;C:\Program Files (x86)\arduino-0017\
"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\javac" -cp "C:\Program Files (x86)\arduino-0017\lib\RXTXcomm.jar" SerialTest.java
"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\java" -cp "C:\Program Files (x86)\arduino-0017\lib\RXTXcomm.jar;." SerialTest
 


read analog input (and show/print to serial terminal/display)
https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor

https://arduino-info.wikispaces.com/QuickRef

Motors

PWM and hbridge motor control

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// --------------------------------------------------------------------------- Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

}

// --------------------------------------------------------------------------- Loop
void loop() {

drive_forward();
delay(1000);
motor_stop();
Serial.println("1");

drive_backward();
delay(1000);
motor_stop();
Serial.println("2");

turn_left();
delay(1000);
motor_stop();
Serial.println("3");

turn_right();
delay(1000);
motor_stop();
Serial.println("4");

motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}

// --------------------------------------------------------------------------- Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}


2 motors
const int pwmOutPin = 9;
const int pwmOutPin2 = 10;
const int pinForward = 7;
const int pinReverse = 8;
const int pinForward2 =12;
const int pinReverse2 = 13;
// control 2 motors on lm293d h-bridge chip
void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(pinForward,OUTPUT);
  pinMode(pinReverse,OUTPUT);
  pinMode(pinForward2,OUTPUT);
  pinMode(pinReverse2,OUTPUT);
}

void loop() {
  //both motors forward at different speeds
  digitalWrite(13, HIGH);
  digitalWrite(pinForward, HIGH);        
  digitalWrite(pinReverse, LOW);        
  digitalWrite(pinForward2, HIGH);        
  digitalWrite(pinReverse2, LOW);        
  analogWrite(pwmOutPin, 85);
  analogWrite(pwmOutPin2, 155);
  delay(3000);

  //both motors stopped
  digitalWrite(13, LOW);        
  analogWrite(pwmOutPin, 0);  
  analogWrite(pwmOutPin2, 0);  
  delay(3000);

  //both motors reverse at different speeds
  digitalWrite(13, HIGH);        
  digitalWrite(pinForward, LOW);        
  digitalWrite(pinReverse, HIGH);        
  digitalWrite(pinForward2, LOW);        
  digitalWrite(pinReverse2, HIGH);        
  analogWrite(pwmOutPin, 85);
  analogWrite(pwmOutPin2, 155);
  delay(3000);

  //both motors stopped
  digitalWrite(13, LOW);        
  analogWrite(pwmOutPin, 0);  
  analogWrite(pwmOutPin2, 0);  
  delay(3000);
}

PWM

Pulse Width Modulation (PWM) is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off.
This on-off pattern can simulate voltages in between
    full on (5 Volts) and
    off (0 Volts)
by changing the portion of the time the signal spends on
    versus the time that the signal spends off.

The duration of "on time" is called the pulse width.
To get varying analog values, you change/modulate, that pulse width.

If you repeat this on-off pattern fast enough with an LED for example,
    the result is as if the signal is a steady voltage between 0-5v controlling the brightness

analogWrite() scale is 0-255, such that:
    analogWrite(myPin, 255) requests a 100% duty cycle (always on),
    analogWrite(myPin, 127) is a 50% duty cycle (on half the time)


// ex: Set LED brightness proportional to a potentiometer:
int ledPin=9;       // LED on digital pin 9
int analogPotPin=3; // potentiometer on analog pin 3
int val=0;          // store value read from pot

void setup(){
  pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop(){
  val = analogRead(analogPotPin);  
  analogWrite(ledPin, val/4);  // Read 0-1023, Write 0-255
}

Make your own board


http://arduino.cc/en/main/standalone

http://www.instructables.com/id/Build-Your-Own-Arduino/


 sketch needs setup and loop at minimum.

 define var
 Include bla.h
*varptr

 pinmode
 digitalwrite low high
 lcd shield

 lcd write dc low cmds high means sending data


Digital pin voltage output 0 to 5vdc Vcc (20ma continuous   40ma max/peak)
   ...likewise for 3.3vdc

Red LED 1.5Vf ...see datasheet for specific leds
  most can be run < 20ma w/decent brightness,


http://arduino.cc/en/Main/ArduinoBoardUno
   Microcontroller ATmega328
   Operating Voltage       5V  ...or 3.3v?
   Input Voltage           7-12V
   Digital I/O Pins        14 (6 are PWM ...pulse-wave-mod=fake_analog)
   Analog Input Pins       6
   DC Current per I/O Pin  40mA ...max?  ...3.3V is 50mA ...max

USB serial comms via putty from pi

#include <LiquidCrystal.h>
#define led 13  // built-in LED
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //init LCD lib w/number of interface pins
int ByteReceived;

void setup(){   /****** SETUP: RUNS ONCE ******/
  Serial.begin(9600); 
  Serial.println("--- Arduino listening on Serial port ---");
  Serial.println("I will echo your input to the LCD display"); 
  Serial.println();
  lcd.begin(16, 2); //cols and rows
  lcd.print("Dockerys Arduino!");
  lcd.setCursor(5, 1); //col10 (pos11)  row1 (aka line2)
  lcd.print("<--- char");
}
void loop(){   /****** LOOP: RUNS CONSTANTLY ******/
  if (Serial.available() > 0)  {
    ByteReceived = Serial.read();
    Serial.print("Arduino confirming rcpt of char:");
    Serial.print(char(ByteReceived));     
    Serial.print(" dec:");
    Serial.print(ByteReceived);  
    Serial.print(" hex:");
    Serial.print(ByteReceived, HEX);
    lcd.setCursor(0, 1); //col0  row/line1
    lcd.print(char(ByteReceived)); //count seconds since last reset

    if(ByteReceived == '1'){ // Single Quote! This is a character.   
      digitalWrite(led,HIGH);
      Serial.print(" LED ON ");
    }   
    if(ByteReceived == '0'){
      digitalWrite(led,LOW);
      Serial.print(" LED OFF");
    }
    Serial.println();    // End the line
  }
}


Map

Arduino analogRead's 1024 levels between 0V and 5V,
 returning int 0 through 1023. For many uses, this is fine.
  but map can scale val back to 0–5, (helpful for measuring volts)

int v2 = map(v1, 0, 1023, 0, 5);
v1 = val to be scaled
0 = start src val
1023 = end src val
0 = start target val
5 = end target val

map has been programmed using integer math. This means that, using the function call above,
if v1 was 498 then v2 will be 2, not 2.43. To increase the accuracy of the converted reading,
a possible solution is to map the value to a range in millivolts instead, 0mV–5000mV, which will return the value 2434mV (2.434V).

like
private long map(long x, long in_min, long in_max, long out_min, long out_max) {
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

/*  Display text then bmp logo file (from microSD card) 

    on TFT touch LCD screen. */
#include <SPI.h>
#include <SD.h>
#include <TFT.h>  //touch LCD lib
TFT TFTscreen = TFT(lcd_cs, dc, rst);
PImage logo;  // this variable represents the image to be drawn on screen
void setup() {
  TFTscreen.begin();
  TFTscreen.background(255, 255, 255);
  TFTscreen.stroke(0, 0, 255);
  TFTscreen.println("Arduino TFT Bitmap Example");
  TFTscreen.stroke(0, 0, 0);
  logo = TFTscreen.loadImage("i.bmp");
  if (!logo.isValid()) {
    Serial.println("error while loading i.bmp");
  }
}

Comments

Popular Posts