Jumat, 17 Juni 2016

Arduino ama Stepper

Waktunya mempelajari stepper

Motor stepper adalah salah satu jenis motor dc yang dikendalikan dengan pulsa-pulsa digital.Prinsip kerja motor stepper adalah bekerja dengan mengubah pulsa elektronis menjadi gerakan mekanis diskrit dimana motor stepper bergerak berdasarkan urutan pulsa yang diberikan kepada motor stepper tersebut.

Prinsip kerja motor stepper adalah mengubah pulsa-pulsa input menjadi gerakan mekanis diskrit. Oleh karena itu untuk menggerakkan motor stepper diperlukan pengendali motor stepper yang membangkitkan pulsa-pulsa periodik.
Berikut ini adalah ilustrasi struktur motor stepper sederhana dan pulasa yang dibutuhkan untuk menggerakkannya :






int motorPin1 = 10;
int motorPin2 = 11;
int motorPin3 = 12;
int motorPin4 = 13;
int d1 = 2;
int x=1; int a,b,c,d;
int y=-1;
void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop() {
if (x==1) {a=1;b=0; c=0; d=0; }
if (x==2) {a=0;b=1; c=0; d=0; }
if (x==3) {a=0;b=0; c=1; d=0; }
if (x==4) {a=0;b=0; c=0; d=1; }
x=x+y ;
if (x>4) x=1;
if (x<1)x=4;
  digitalWrite(motorPin1, a);
  digitalWrite(motorPin2, b);
  digitalWrite(motorPin3, c);
  digitalWrite(motorPin4, d);
  delay(d1);
}

Selasa, 14 Juni 2016

Running Led


Coba perhatikan gambar di atas sebentar saja.. ya coba amati dengan seksama, Gambarnya kurang gaul. ga pake software terkini sich.. yaaa.. itu memang benar , tetapi bukan itu maksudnya. Gambar di atas adalah rangkaian yang akan kita praktekan hari ini. kita akan membuat kreasi program biasa saja dan sedikit di beri bumbu tambahan agar lebih menarik


Nah sekarang cobalah program di bawah.
-------------------------------------------------------------------------------
int lb=13;
int a; int b=2; int c =1;
void setup() {
pinMode (2,OUTPUT) ;  pinMode (3,OUTPUT) ;
pinMode (4,OUTPUT) ;  pinMode (5,OUTPUT) ;
pinMode (6,OUTPUT) ; pinMode (lb,OUTPUT) ;
}
void loop() {
digitalWrite (lb, a); a=a+1;
if (a>1) a=0;
digitalWrite (b, c); b=b+1;
if (b>6) { b=2; 
   if (c==1) c=0;
   else c=1; }
delay(500);
}

-------------------------------------------------------------------------------
Sketsa program di atas akan menghasilkan lampu berjalan pada led yang dihubungkan dengan kaki 2,3,4,5&6 . Sedangkan led yang dihubungkan dengan kaki nomor 13 adalah led berkedip.
 Nah Sekarang cobalah Program di bawah. Lihat apa perbedaannya.
-------------------------------------------------------------------------------
int lb=13;
int a=1; int b=2; int c =1; int d=1;
void setup() {
pinMode (2,OUTPUT) ;  pinMode (3,OUTPUT) ;
pinMode (4,OUTPUT) ;  pinMode (5,OUTPUT) ;
pinMode (6,OUTPUT) ; pinMode (lb,OUTPUT) ;
}
void loop() {
digitalWrite (lb, a); 
d=d+1;
if (d==11 ) { d=1; a=a+1;
if (a>1) a=0;} 
digitalWrite (b, c); b=b+1;
if (b>6) { b=2; 
   if (c==1) c=0;
   else c=1; }
delay(100);
}
-------------------------------------------------------------------------------
Hasilnya ternyata sama saja ya.. hihihi.. Hanya saja lebih cepat
Nah Bagaiman dengan Sketsa program di bawah...
-------------------------------------------------------------------------------
int lb=13;
int a=1; int b=2; int c =1; int d=1;
void setup() {
pinMode (2,OUTPUT) ;  pinMode (3,OUTPUT) ;
pinMode (4,OUTPUT) ;  pinMode (5,OUTPUT) ;
pinMode (6,OUTPUT) ; pinMode (lb,OUTPUT) ;
}
void loop() {
digitalWrite (lb, a);
d=d+1;
if (d==11 ) { d=1; a=a+1;
if (a>1) a=0;}
digitalWrite (b-1, 0); digitalWrite (b, 1); b=b+1;
if (b>7) { b=2;
 }
delay(200);
}
-------------------------------------------------------------------------------
Untuk Latihan sekarang tolong pindahkan led yang nomor 13 dipindahkan ke kaki no 7 yaaa...
coba Upload program di bawah ini dan perhatikan hasilnya..  ayo berlatih terus dengan semangat yaaa....
-------------------------------------------------------------------------------
void setup() {
pinMode (2,OUTPUT) ;
pinMode (3,OUTPUT) ;
pinMode (4,OUTPUT) ;
pinMode (5,OUTPUT) ;
pinMode (6,OUTPUT) ;
pinMode (7,OUTPUT) ;
}

void loop() {
for (int x=2; x<8 ; x++ ) {
  digitalWrite (x,HIGH); delay (100);}
for (int z=1; z<5 ; z++) {
for (int x=2; x<8 ; x++ ) {
  digitalWrite (x,HIGH); }
  delay (100) ;
for (int x=2; x<8 ; x++ ) {
  digitalWrite (x,LOW); }
  delay (100);
} }



-------------------------------------------------------------------------------

Minggu, 12 Juni 2016

berkreasi dengan arduino &saklar plus led

DC motors are used for all sort of robotic projects.

The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!

Note that the H-bridge chip is not meant for driving continuous loads of 1.2A, so this is for small motors. Check the datasheet for information about the motor to verify its OK!

Connecting DC Motors

To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch

Include the required libraries

Make sure you #include the required libraries
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Create the Adafruit_MotorShield object

 Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Create the DC motor object

Request the DC motor from the Adafruit_MotorShield:
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
with getMotor(port#)Port# is which port it is connected to. If you're using M1 its 1, M2 use 2, M3 useand M4 use 4

Connect to the Controller

In your setup() function, call 'begin()" on the Adafruit_MotorShield object:
AFMS.begin();

Set default speed

Set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
myMotor->setSpeed(100);

Run the motor

To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.
myMotor->run(FORWARD);












/* YourDuinoStarter Example: 2 Stepper Motors
 - WHAT IT DOES: Runs 2 28BYJ-48 stepper motors with AccelStepper Library
 - Motors accelerate and decelerate simultaneously in opposite rotations
 - SEE the comments after "//" on each line below
 -  Derived from example code by Mike McCauley
 -  modified by Celem for single stepper
 -  modified by lowres for two steppers 
 NOTE: This may not run 2 motors from USB. 
       May need separate +5 Supply for motors
 - CONNECTIONS: See Pin definitions below

 - V1.01 11/30/2013
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <AccelStepper.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define FULLSTEP 4
#define HALFSTEP 8
// motor pins
#define motorPin1  4     // Blue   - 28BYJ48 pin 1
#define motorPin2  5     // Pink   - 28BYJ48 pin 2
#define motorPin3  6     // Yellow - 28BYJ48 pin 3
#define motorPin4  7     // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
                        
#define motorPin5  8     // Blue   - 28BYJ48 pin 1
#define motorPin6  9     // Pink   - 28BYJ48 pin 2
#define motorPin7  10    // Yellow - 28BYJ48 pin 3
#define motorPin8  11    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
/*-----( Declare objects )-----*/
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

/*-----( Declare Variables )-----*/
//none

void setup()   /****** SETUP: RUNS ONCE ******/
{
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(2048);  // 1 revolution 
  
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(50.0);
  stepper2.setSpeed(200);
  stepper2.moveTo(-2048);  // 1 revolution 

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //Change direction at the limits
  if (stepper1.distanceToGo() == 0) 
    stepper1.moveTo(-stepper1.currentPosition());
    if (stepper2.distanceToGo() == 0) 
    stepper2.moveTo(-stepper2.currentPosition());
  
  stepper1.run();
  stepper2.run();

}//--(end main loop )---














Rabu, 08 Juni 2016

robot mobil

#define a1 6  //motor 1
#define a2 7
#define b1 8  // motor 2
#define b2 9
int kond = 5; int bt=5; int b=255;
int a=255; int c=255; int d=255; int x=255;
void setup()
{
  Serial.begin( 9600 );
  pinMode( a1, OUTPUT );
  pinMode( a2, OUTPUT );
  pinMode( b1, OUTPUT );
  pinMode( b2, OUTPUT );
}

void loop()
{
//mundur
  analogWrite( a1, 222 ); analogWrite( a2, 0 );
  analogWrite( b1, 0 ); analogWrite( b2, 0 );

// maju
  delay(2133);
  analogWrite( a1, 0 ); analogWrite( a2, 222 );
  analogWrite( b1, 0 ); analogWrite( b2, 0 );


  delay(2133);
  analogWrite( a1, 0 ); analogWrite( a2, 0 );
  analogWrite( b1, 222 ); analogWrite( b2, 0 );


  delay(2133);
  analogWrite( a1, 0 ); analogWrite( a2, 0 );
  analogWrite( b1, 0 ); analogWrite( b2, 222 );


  delay(2133);
  analogWrite( a1, 0 ); analogWrite( a2, 0 );
  analogWrite( b1, 0 ); analogWrite( b2, 0 );


  delay(2133);
}



Puasa hari ke empat. 9 Juni 2016. Hari kamis yang cerah. Saatnya mulai percobaan untuk membuat robot yang dapat dikendalikan melalui  bluetooth dengan ponsel android sebagai pengendali.
percobaan 1 maju mundur cantik

#define a1 10 // D10 --> Motor B Input A --> MOTOR B +
#define a2 11 // D11 --> Motor B Input B --> MOTOR B -
#define b1 8 // D10 --> Motor B Input A --> MOTOR B +
#define b2 9 // D11 --> Motor B Input B --> MOTOR B -
int kond = 5; int bt=5;
void setup()
{
  Serial.begin( 9600 );
  pinMode( a1, OUTPUT );
  pinMode( a2, OUTPUT );
  pinMode( b1, OUTPUT );
  pinMode( b2, OUTPUT );
}

void loop()
{
if( Serial.available() >0 ) {
  bt= Serial.read (); }
if ( bt==1 ){
  if (kond=2){
  digitalWrite( a2, 0 ); digitalWrite( b2, 0 );
  delay(55); kond = 1;}
  digitalWrite( a1, 1 ); digitalWrite( a2, 0 );
  digitalWrite( b1, 1 ); digitalWrite( b2, 0 );
  delay(200);
}

if ( bt==2 ){
  if (kond=1){
  digitalWrite( a1, 0 ); digitalWrite( b1, 0 );
  delay(55);kond=2; }
  digitalWrite( a1, 0 ); digitalWrite( a2, 1);
  digitalWrite( b1, 0 ); digitalWrite( b2, 1);
  delay(200);
}
if ( bt==5 ){
  digitalWrite( a1, 0 ); digitalWrite( a2, 0 );
  digitalWrite( b1, 0 ); digitalWrite( b2, 0);
  delay(200);
}
delay(333);
}



belum di coba
program komplit

#define a1 10 // D10 --> Motor B Input A --> MOTOR B +
#define a2 11 // D11 --> Motor B Input B --> MOTOR B -
#define b1 8 // D10 --> Motor B Input A --> MOTOR B +
#define b2 9 // D11 --> Motor B Input B --> MOTOR B -
int kond = 5; int bt=5; int b=255;
int a=255; int c=255; int d=255; int x=255;
void setup()
{
  Serial.begin( 9600 );
  pinMode( a1, OUTPUT );
  pinMode( a2, OUTPUT );
  pinMode( b1, OUTPUT );
  pinMode( b2, OUTPUT );
}

void loop()
{
if( Serial.available() >0 ) {
  bt= Serial.read (); 
  }
  if (bt==6) x=255; //cepat
  if (bt==7) x=170; // Sedang
  if (bt==8) x=122; 
if ( bt==1 ){ // Maju
  if (kond==2){
  a=0; b=0; c=0; d= 0;
  delay(115); kond = 1;}
  a=x; b=0; c=x; d= 0;
  delay(200);
}

if ( bt==2 ){
  if (kond==1){
  a=0; b=0; c=0; d= 0;
  delay(115);kond=2; }
  a=0; b=x; c=0; d= x;
  delay(200);
}
if ( bt==5 ){
  a=0; b=0; c=0; d= 0;
  delay(200); kond=5;
}

if ( bt==4){ //belok kiri
  if (kond==1 ){
  a=100; b=0; c=255; d= 0; }
  if (kond==2 ){
  a=0; b=100; c=255; d= 0; }
  if (kond==5 ){
  a=0; b=0; c=255; d= 0; }
  delay(55);kond=4; }
if ( bt==3){ //belok kanan
  if (kond==1 ){
  a=255; b=0; c=111; d= 0; }
  if (kond==2 ){
  a=0; b=255; c=0; d= 110; }
  if (kond==5 ){
  a=255; b=0; c=0; d= 0; }
  delay(55);kond=3; }

  analogWrite( a1, a ); analogWrite( a2, b );
  analogWrite( b1, c ); analogWrite( b2, d );


  delay(133);
}




masuk line followr

#define a1 10 // D10 --> Motor B Input A --> MOTOR B +
#define a2 11 // D11 --> Motor B Input B --> MOTOR B -
#define b1 8 // D10 --> Motor B Input A --> MOTOR B +
#define b2 9 // D11 --> Motor B Input B --> MOTOR B -
int kond = 5; int bt=5; int b=255;
int a=255; int c=255; int d=255; int x=255;
int s1,s2;
void setup()
{
  Serial.begin( 9600 );
  pinMode( a1, OUTPUT );
  pinMode( a2, OUTPUT );
  pinMode( b1, OUTPUT );
  pinMode( b2, OUTPUT );
}

void loop()
{
if( Serial.available() >0 ) {
  bt= Serial.read (); 
  }
  if (bt !=9) { // Jalan Manual
  
  if (bt==6) x=255; //cepat
  if (bt==7) x=170; // Sedang
  if (bt==8) x=122; 
if ( bt==1 ){ // Maju
  if (kond==2){
  a=0; b=0; c=0; d= 0;
  delay(115); kond = 1;}
  a=x; b=0; c=x; d= 0;
  delay(200);
}

if ( bt==2 ){
  if (kond==1){
  a=0; b=0; c=0; d= 0;
  delay(115);kond=2; }
  a=0; b=x; c=0; d= x;
  delay(200);
}
if ( bt==5 ){
  a=0; b=0; c=0; d= 0;
  delay(200); kond=5;
}

if ( bt==4){ //belok kiri
  if (kond==1 ){
  a=100; b=0; c=255; d= 0; }
  if (kond==2 ){
  a=0; b=100; c=255; d= 0; }
  if (kond==5 ){
  a=0; b=0; c=255; d= 0; }
  delay(55);kond=4; }
if ( bt==3){ //belok kanan
  if (kond==1 ){
  a=255; b=0; c=111; d= 0; }
  if (kond==2 ){
  a=0; b=255; c=0; d= 110; }
  if (kond==5 ){
  a=255; b=0; c=0; d= 0; }
  delay(55);kond=3; }

  analogWrite( a1, a ); analogWrite( a2, b );
  analogWrite( b1, c ); analogWrite( b2, d );
  delay(133); } // Akhir Jalan Manual
  
  if (bt==9 ) {
    s1= analogRead (A0); s2= analogRead (A1);
 // s1 kiri  s2 kanan
  if (s1<111 && s2<111 } // maju
  if (s1<111 && s2>811 } // Kanan
  if (s1>811 && s2<111 } // Kiri
  if (s1>811 && s2>811 } // stop

}}







Jumat, 03 Juni 2016

servo arduino dan pengendalian bluetooth


Percobaan satu

 #include <Servo.h>
Servo myservo;
int pos = 0;  
int n;
void setup()
{
Serial.begin(9600);
  myservo.attach(9);
  for(pos = 30; pos>0; pos-=1)  
  {                            
    myservo.write(pos);            
    delay(15);                    
  }
delay(888);}
void loop()
{
if( Serial.available() >0 ) {
     n = Serial.read();
      if ( n==1 && pos == 90) {
      for(pos = 90; pos>=1; pos-=1)  
  {   myservo.write(pos); delay(10); } }                  
   if (n==2 && pos== 0) {
  for(pos = 0; pos<90; pos  +=1)  
  { myservo.write(pos); delay(10);  } }                
   
} }




Percobaan 1 Line Follower
Percobaan pertama adalah menjalankan sensor line follower nya. Sensor line folower kita hubungkan dengan pin analog A0. Dan kita perhatikan pembacaan sensor nya dengan cara dekatkan sensor dengan warna hitam dan kemudian dekatkan dengan warn pautih

void setup() {  Serial.begin (9600);  }
void loop() {
Serial.println (analogRead (A1));
delay (1100);   }