push button
// Arduino pin numbers
const int SW_pin = 7; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
delay(500);
}
///////////////////////////////////////////////////////////////////////////
// wired connections
#define B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
// functional connections
#define MOTOR_B_PWM B_IA // Motor B PWM Speed
#define MOTOR_B_DIR B_IB // Motor B Direction
// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 50 // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes
// joystick pins
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup()
{
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin( 115200 );
Serial.begin( 115200 );
pinMode( MOTOR_B_DIR, OUTPUT );
pinMode( MOTOR_B_PWM, OUTPUT );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
}
void loop()
{
boolean isValidInput;
// draw a menu on the serial port
Serial.println( "-----------------------------" );
Serial.println( "MENU:" );
Serial.println( "1) Fast forward" );
Serial.println( "2) Forward" );
Serial.println( "3) Soft stop (coast)" );
Serial.println( "4) Reverse" );
Serial.println( "5) Fast reverse" );
Serial.println( "6) Hard stop (brake)" );
Serial.println( "-----------------------------" );
do
{
byte c;
// get the next character from the serial port
Serial.print( "?" );
while( !Serial.available() )
; // LOOP...
c = Serial.read();
// execute the menu option based on the character recieved
switch( c )
{
case '1': // 1) Fast forward
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
isValidInput = true;
break;
case '2': // 2) Forward
Serial.println( "Forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_SLOW ); // PWM speed = slow
isValidInput = true;
break;
case '3': // 3) Soft stop (preferred)
Serial.println( "Soft stop (coast)..." );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
isValidInput = true;
break;
case '4': // 4) Reverse
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
analogWrite( MOTOR_B_PWM, PWM_SLOW ); // PWM speed = slow
isValidInput = true;
break;
case '5': // 5) Fast reverse
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
analogWrite( MOTOR_B_PWM, PWM_FAST ); // PWM speed = fast
isValidInput = true;
break;
case '6': // 6) Hard stop (use with caution)
Serial.println( "Hard stop (brake)..." );
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
isValidInput = true;
break;
default:
// wrong character! display the menu again!
isValidInput = false;
break;
}
} while( isValidInput == true );
// repeat the main loop and redraw the menu...
}
///////////////////////////////////////////////////////////////////////////////////////////
// normal
X-axis: 502
Y-axis: 521
// Switch: up towrds leeter
X-axis: 0
Y-axis: 521
// switch : down opposite of letters
X-axis: 1023
Y-axis: 521
//switch
Switch:right
X-axis: 502
Y-axis: 0
//switch left
Switch: 1
X-axis: 502
Y-axis: 1023
//////////////////////////////////////////////////////////////////////////////////////
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int M1_Left = 8; //Motor Input 1
int M1_Right = 9; //Motor Input 2
int M2_Left = 11; //Motor Input 1
int M2_Right = 12; //Motor Input 2
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
delay(500);
if (analogRead(X_pin) >520)
{
forward(255);
}
else if (analogRead(X_pin)<470)
{
reverse(255);
}
else if ( analogRead(Y_pin)>500)
{
analogWrite (M2_Left, 0); //Motor Input 1
analogWrite(M2_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else if ( analogRead(Y_pin)<485)
{
analogWrite (M1_Left, 0); //Motor Input 1
analogWrite(M1_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else
stop();
}
void forward (int rate)
{
int inPin1 = LOW;
int inPin2 = rate;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
delay(1000);
}
void reverse (int rate)
{
int inPin1 = rate;
int inPin2 = LOW;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
delay (1000);
}
void stop()
{
int inPin1 = LOW;
int inPin2 = LOW;
digitalWrite(M1_Left, LOW);
digitalWrite(M1_Right , LOW);
digitalWrite(M2_Left, LOW);
digitalWrite(M2_Right , LOW);
}
const int SW_pin = 7; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
delay(500);
}
///////////////////////////////////////////////////////////////////////////
// wired connections
#define B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
// functional connections
#define MOTOR_B_PWM B_IA // Motor B PWM Speed
#define MOTOR_B_DIR B_IB // Motor B Direction
// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 50 // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes
// joystick pins
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup()
{
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin( 115200 );
Serial.begin( 115200 );
pinMode( MOTOR_B_DIR, OUTPUT );
pinMode( MOTOR_B_PWM, OUTPUT );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
}
void loop()
{
boolean isValidInput;
// draw a menu on the serial port
Serial.println( "-----------------------------" );
Serial.println( "MENU:" );
Serial.println( "1) Fast forward" );
Serial.println( "2) Forward" );
Serial.println( "3) Soft stop (coast)" );
Serial.println( "4) Reverse" );
Serial.println( "5) Fast reverse" );
Serial.println( "6) Hard stop (brake)" );
Serial.println( "-----------------------------" );
do
{
byte c;
// get the next character from the serial port
Serial.print( "?" );
while( !Serial.available() )
; // LOOP...
c = Serial.read();
// execute the menu option based on the character recieved
switch( c )
{
case '1': // 1) Fast forward
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
isValidInput = true;
break;
case '2': // 2) Forward
Serial.println( "Forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_SLOW ); // PWM speed = slow
isValidInput = true;
break;
case '3': // 3) Soft stop (preferred)
Serial.println( "Soft stop (coast)..." );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
isValidInput = true;
break;
case '4': // 4) Reverse
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
analogWrite( MOTOR_B_PWM, PWM_SLOW ); // PWM speed = slow
isValidInput = true;
break;
case '5': // 5) Fast reverse
Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
analogWrite( MOTOR_B_PWM, PWM_FAST ); // PWM speed = fast
isValidInput = true;
break;
case '6': // 6) Hard stop (use with caution)
Serial.println( "Hard stop (brake)..." );
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
isValidInput = true;
break;
default:
// wrong character! display the menu again!
isValidInput = false;
break;
}
} while( isValidInput == true );
// repeat the main loop and redraw the menu...
}
///////////////////////////////////////////////////////////////////////////////////////////
// normal
X-axis: 502
Y-axis: 521
// Switch: up towrds leeter
X-axis: 0
Y-axis: 521
// switch : down opposite of letters
X-axis: 1023
Y-axis: 521
//switch
Switch:right
X-axis: 502
Y-axis: 0
//switch left
Switch: 1
X-axis: 502
Y-axis: 1023
//////////////////////////////////////////////////////////////////////////////////////
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int M1_Left = 8; //Motor Input 1
int M1_Right = 9; //Motor Input 2
int M2_Left = 11; //Motor Input 1
int M2_Right = 12; //Motor Input 2
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
delay(500);
if (analogRead(X_pin) >520)
{
forward(255);
}
else if (analogRead(X_pin)<470)
{
reverse(255);
}
else if ( analogRead(Y_pin)>500)
{
analogWrite (M2_Left, 0); //Motor Input 1
analogWrite(M2_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else if ( analogRead(Y_pin)<485)
{
analogWrite (M1_Left, 0); //Motor Input 1
analogWrite(M1_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else
stop();
}
void forward (int rate)
{
int inPin1 = LOW;
int inPin2 = rate;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
delay(1000);
}
void reverse (int rate)
{
int inPin1 = rate;
int inPin2 = LOW;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
delay (1000);
}
void stop()
{
int inPin1 = LOW;
int inPin2 = LOW;
digitalWrite(M1_Left, LOW);
digitalWrite(M1_Right , LOW);
digitalWrite(M2_Left, LOW);
digitalWrite(M2_Right , LOW);
}
Comments
Post a Comment