2020年1月21日火曜日

Arduino Challenge02


#include <Stepper.h>
const int stepsPerRevolution=200;

Stepper myStepper(stepsPerRovolution,8,9,10,11);
int stepCount=0;
void setup() {
  //nothing to do inside the setup
}

void loop() {
  int sensorReading=analogRead(A0);
  int motorSpeed=map(sensorReading,0,1023,0,100);
  if(motorSpeed > 0 {
    myStepper.setSpeed(motorSpeed);
    myStepper.step(stepsPerRevolution / 100);
  }

}

Arduino Challenge06

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

RobotCarMeimei5 2DCMotorwizL293D

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

// DC motor on M1,M2
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup() { 
  // turn on motor #1,#2
  motor1.setSpeed(255);
  motor1.run(RELEASE);
  motor2.setSpeed(255);
  motor2.run(RELEASE);
}
int i;

void loop() {
  //Forward
  i=255;
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(10000);

  //Back
  i=255;
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(10000);

 //Forward
  i=255;
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(15000);
 
  //Turn Right
  i=255;
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(15000);

  //Forward
  i=255;
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(10000);

  //Turn Left
  i=255;
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor1.setSpeed(i); 
  motor2.setSpeed(i);
  delay(15000);

 
 






}

Arduino Challenge05

RobotCarMeimei4 2DCGearMotor wiz JoyStick
RobotCarMeimei4 2DCGearMotor wiz JoyStick

RobotCarMeimei4 2DCGearMotor wiz JoyStic

RobotCarMeimei4 2DCGearMotor wiz JoyStic



/*
Control 2 DC motors uing JoyStick And Volume
2020/01/11 Start
2020/01/11 Up
 */

int MotorPinA=12;
int MotorSpeedPinA=10;
int MotorBrakePinA=9;

int MotorPinB=13;
int MotorSpeedPinB=11;
int MotorBrakePinB=8;

int MotorSpeedA;
int MotorSpeedB;

int SwitchPin =2 ; // digital pin connected to switch output
int joystick_x = A0 ; // analog pin connected to X output
int joystick_y = A1 ; // analog pin connetcte to Y outpu
int pos_x=0;
int pos_y=0;
int btState=0;

int analogIn,vrefValue;

void setup()
{
  // motor A pin assignment
  pinMode(MotorPinA,OUTPUT);
  pinMode(MotorSpeedPinA,OUTPUT);
  pinMode(MotorBrakePinA,OUTPUT);
  // motor B pin assignment
  pinMode(MotorPinB,OUTPUT);
  pinMode(MotorSpeedPinB,OUTPUT);
  pinMode(MotorBrakePinB,OUTPUT);
  // joystick pin assignment
  pinMode(SwitchPin,INPUT);
  pinMode(joystick_x,INPUT);
  pinMode(joystick_y,INPUT);
  //Speed
  analogIn=analogRead(2);
  vrefValue=map(analogIn,0,1023,100,255);
  MotorSpeedA=vrefValue;
  MotorSpeedB=vrefValue;
}

//PushButton Forward
void MotorForward(void)
  {
  digitalWrite(MotorPinA,LOW);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,HIGH);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton Back
void MotorBack(void)
  {
  digitalWrite(MotorPinA,HIGH);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,LOW);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton TurnR
void MotorTurnR(void)
  {
  digitalWrite(MotorPinA,HIGH);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,HIGH);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton TurnL
void MotorTurnL(void)
  {
  digitalWrite(MotorPinA,LOW);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,LOW);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

void MotorStop(void)
  {
    digitalWrite(MotorBrakePinA,HIGH);
    digitalWrite(MotorBrakePinB,HIGH);
    analogWrite(MotorSpeedPinA,0);
    analogWrite(MotorSpeedPinB,0);
  }

void loop()
{
  btState=digitalRead(SwitchPin);
  if(btState==HIGH)
  {
    MotorStop();
  }

  pos_x=analogRead(joystick_x);
  pos_y=analogRead(joystick_y);

  if(pos_y<400)
  {
    MotorForward();
  }

  else if (pos_y>600)
  {
    MotorBack();
  }

  else if (pos_x>600)
  {
    MotorTurnR();
  }

  else if (pos_x<400)
  {
    MotorTurnL();
  }

  else if ((pos_y>400)&&(pos_y<600))
  {
    MotorStop();
  }






}

Arduino Challenge04

RobotCarMeimei3 2DCGearMotor wiz IR

RobotCarMeimei3 2DCGearMotor wiz IR

RobotCarMeimei3 2DCGearMotor wiz IR

RobotCarMeimei3 2DCGearMotor wiz IR



#include <AFMotor.h>
#include <IRremote.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

int RecvPin = 9;
IRrecv irrecv(RecvPin);
decode_results results;

void setup()
{
  motor1.setSpeed(255);
  motor1.run(RELEASE);
  motor2.setSpeed(255);
  motor2.run(RELEASE);

 irrecv.enableIRIn();
}

void loop()
{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
    if (results.value == 0xFF18E7)
    {
      motor1.setSpeed(200);
      motor2.setSpeed(200);
      motor1.run(BACKWARD);
      motor2.run(FORWARD);
    }
    if (results.value == 0xFF4AB5)
    {
      motor1.setSpeed(255);
      motor2.setSpeed(255);
      motor1.run(FORWARD);
      motor2.run(BACKWARD);
    }
    if (results.value == 0xFF38C7)
    {
      motor1.run(RELEASE);
      motor2.run(RELEASE);
    }
    if (results.value == 0xFF5AA5)
    {
      motor1.setSpeed(255);
      motor2.setSpeed(255);
      motor1.run(RELEASE);
      motor2.run(FORWARD);
      //delay(200);
     // MotorStop();
    }
    if (results.value == 0xFF10EF)
    {
      motor1.setSpeed(255);
      motor2.setSpeed(255);
      motor1.run(FORWARD);
      motor2.run(RELEASE);
      //delay(200);
      //MotorStop();
    }
  }
}

Arduino Challenge03

Control 2 DC motors using JoyStick

Control 2 DC motors using JoyStick



/*
ChallengeMotor5
Control 2 DC motors using JoyStick And Volume
2019/12/22 Start
 */

int MotorPinA=12;
int MotorSpeedPinA=10;
int MotorBrakePinA=9;

int MotorPinB=13;
int MotorSpeedPinB=11;
int MotorBrakePinB=8;

int MotorSpeedA=255;
int MotorSpeedB=255;

int SwitchPin =2 ; // digital pin connected to switch output
int joystick_x = A0 ; // analog pin connected to X output
int joystick_y = A1 ; // analog pin connetcte to Y outpu
int pos_x=0;
int pos_y=0;
int btState=0;

void setup()
{
  // motor A pin assignment
  pinMode(MotorPinA,OUTPUT);
  pinMode(MotorSpeedPinA,OUTPUT);
  pinMode(MotorBrakePinA,OUTPUT);
  // motor B pin assignment
  pinMode(MotorPinB,OUTPUT);
  pinMode(MotorSpeedPinB,OUTPUT);
  pinMode(MotorBrakePinB,OUTPUT);
  // joystick pin assignment
  pinMode(SwitchPin,INPUT);
  pinMode(joystick_x,INPUT);
  pinMode(joystick_y,INPUT); 
}

//PushButton Forward
void MotorForward(void)
  {
  digitalWrite(MotorPinA,LOW);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,HIGH);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton Back
void MotorBack(void)
  {
  digitalWrite(MotorPinA,HIGH);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,LOW);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton TurnR
void MotorTurnR(void)
  {
  digitalWrite(MotorPinA,HIGH);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,HIGH);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

//PushButton TurnL
void MotorTurnL(void)
  {
  digitalWrite(MotorPinA,LOW);
  digitalWrite(MotorBrakePinA,LOW);
  analogWrite(MotorSpeedPinA,MotorSpeedA);
  digitalWrite(MotorPinB,LOW);
  digitalWrite(MotorBrakePinB,LOW);
  analogWrite(MotorSpeedPinB,MotorSpeedB);
  }

void MotorStop(void)
  {
    digitalWrite(MotorBrakePinA,HIGH);
    digitalWrite(MotorBrakePinB,HIGH);
    analogWrite(MotorSpeedPinA,0);
    analogWrite(MotorSpeedPinB,0);
  }

void loop()
{
  btState=digitalRead(SwitchPin);
  if(btState==HIGH)
  {
    MotorStop();
  }

  pos_x=analogRead(joystick_x);
  pos_y=analogRead(joystick_y);

  if(pos_y<400)
  {
    MotorForward();
  }

  else if (pos_y>600)
  {
    MotorBack();
  }

  else if (pos_x>600)
  {
    MotorTurnR();
  }

  else if (pos_x<400)
  {
    MotorTurnL();
  }

  else if ((pos_y>400)&&(pos_y<600))
  {
    MotorStop();
  }






}

2020年1月20日月曜日

Arduino Challenge01



DCMotor,Stepper And Servo Using WizL293D



// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>
#include <Servo.h>

// DC motor on M1,M2
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
// DC hobby servo
Servo servo1;
// Stepper motor on M3+M4 48 steps per revolution
AF_Stepper stepper(48, 2);

void setup() {
  //Serial.begin(9600);           // set up Serial library at 9600 bps
  //Serial.println("Motor party!");

  // turn on servo
  servo1.attach(9);
 
  // turn on motor #2
  motor1.setSpeed(200);
  motor1.run(RELEASE);
  motor2.setSpeed(200);
  motor2.run(RELEASE);
}

int i;

// Test the DC motor, stepper and servo ALL AT ONCE!
void loop() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  for (i=0; i<255; i++) {
    servo1.write(i);
    motor1.setSpeed(i);
    motor2.setSpeed(i);
    stepper.step(1, FORWARD, INTERLEAVE);
    delay(3);
 }

  for (i=255; i!=0; i--) {
    servo1.write(i-255);
    motor1.setSpeed(i);
    motor2.setSpeed(i);
    stepper.step(1, BACKWARD, INTERLEAVE);
    delay(3);
 }

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  for (i=0; i<255; i++) {
    servo1.write(i);
    motor1.setSpeed(i);
    motor2.setSpeed(i);
    delay(3);
    stepper.step(1, FORWARD, DOUBLE);
 }

  for (i=255; i!=0; i--) {
    servo1.write(i-255);
    motor1.setSpeed(i);
    motor2.setSpeed(i);
    stepper.step(1, BACKWARD, DOUBLE);
    delay(3);
 }
}



Arduino Trainer



Arduino Trainer1
Arduino Trainer2