Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int enable2 = 9;
- const int Xpin = A0; //analog pin connected to x output
- const int Ypin = A1; //analog pin connected to y output
- /* L298N motor drive control pins */
- int IN1 = 7; // IN1
- int IN2 = 8; // IN2
- /* I'm assuming this function is automatically at runtime like loop() is */
- void setup(){
- /* motor control pins as output */
- pinMode(enable2,OUTPUT);
- pinMode(IN1,OUTPUT);
- pinMode(IN2,OUTPUT);
- Serial.begin(9600);
- }
- void loop(){
- /* Declaring variables to store pin reads */
- int x_val, y_val;
- /* Variables to hold what I assume are the direction/power settings to the motor
- * I would change these to better represent what they actually are. Don't forget
- * to also change their occurances further down though
- */
- int power, dir1, dir2;
- /* perform reads */
- x_val = analogRead(Xpin);
- y_val = analogRead(Ypin);
- /* print values */
- Serial.print("Xpin: ");
- Serial.println(x_val);
- Serial.print("Ypin: ");
- Serial.println(y_val);
- /* are you purposely going for delay between the read and the output to the motor?
- * if not, that's what is happening here. If you're just trying to delay the next iteration
- * of the loop I would move this statement to the last statement of this function
- */
- delay(50);
- /* I'm assuming you want anything over 800 to be fast forward since the range
- * that anologRead() returns will be between 0 and 1023 Xpin > 1023 will never be true
- *
- * Also I would double check these analog and digital values. I don't know what the motor drive
- * expects but I would think that logically both the "forward" options should share something in
- * common on the digital pins whereas analog would carry power level, and the two backward ones
- * likewise respectively. I could be wrong, but I would check.
- */
- if(x_val > 800)
- {
- /* Fast Forward */
- power = 100;
- dir1 = HIGH;
- dir2 = LOW;
- }
- else if(x_val > 605)
- {
- /* Slow Forward */
- power = 150;
- dir1 = LOW;
- dir2 = HIGH;
- }
- else if(x_val > 270)
- {
- /* Slow Backward */
- power = 170;
- dir1 = LOW;
- dir2 = HIGH;
- }
- else /* we don't need another if statement because 0 - 269 is the only option left */
- {
- /* Fast Backward */
- power = 230;
- dir1 = HIGH;
- dir2 = LOW;
- }
- analogWrite(enable2, power);
- digitalWrite(IN1, dir1);
- digitalWrite(IN2, dir2);
- /*
- if(605 < Xpin > 800 ) slowForward();
- if(800 < Xpin > 1023) fastforward();
- if(605 < Xpin > 270) slowbackward();
- if(270 < Xpin > 0) fastbackward();
- */
- }
- /*
- void slowForward(){
- analogWrite(enable2,100);
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW);
- }
- void fastforward(){
- analogWrite(enable2,150);
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,HIGH);
- }
- void slowbackward(){
- analogWrite(enable2,170);
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,HIGH);
- }
- void fastbackward(){
- analogWrite(enable2,230);
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement