Pages: 1 2 [3]   Go Down
Author Topic: how can i send 2 variables to a motor + encoder via h bridge  (Read 2574 times)
0 Members and 1 Guest are viewing this topic.
Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
however i still have the same problems with the pots being displayed.. and the switch on pot 16 nt being noticed by the if statements..
Then I suspect that the playback routine is not being reached. What does your debug printing tell you?
If you have a serial begin in the setup() and then put them in your playback routine like this:-
Code:
void playback()
{
lcd.clear();
lcd.setCursor (4,0);
lcd.print("PLAYBACK");

 Serial.println("in playback");
  if (digitalRead(16) == HIGH) 
   {
     Serial.println(" button pressed");
    lcd.setCursor (4,0);
    lcd.print("POINT A");
    speed1 = count1/time_1;
    digitalWrite (enablePin, HIGH);
    int val = analogRead(sensorPin);                    // read value from pot at sensorPin
    drive1 = map(speed1, 0, 500, 0, 225);     
   
    if (count1 >(0))
      {
      digitalWrite(motor1Pin, drive1);
      digitalWrite(motor2Pin, LOW);
      }
    if (count1 <(0))
      {
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, drive1);
      }
    if (count1 == count1)
      {
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      }
   }

 }

You should see in the monitor a constant stream of
in playback
in playback
in playback
If not you are getting stuck some where else.
Then when you press the button it should print
in playback
button pressed
in playback
button pressed
over and over. If it does this then the if() statement is working.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

iv tryed what you said and i am not getting anything on the monitor when the button is pressed...

i have noticed that i have been goin to the playback subroutine from a previous subroutine.. is there a function that i can use from the previous subroutine to go to void loop() and i can try running the playback subroutine from there as currently it is running from a previous subroutine in void setup()...

could this be an issue??
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is not clear what you are getting. Do you see the in playback message? Exactly what happens when the button is pressed.

No don't call loop() directly find out what is wrong with your code.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,

i have managed to get the two variables:

distance: determined by the encoder on the motor (can be a + or - value therefore "abs" function is used)
time: using a pot

to be stored the issue is that i am using the speed = distance / time in order to rotate the motor and then stop it at the required position.
the issue is that the speed is not accurate and the motor rotates at full speed in forward or reverse depending on the distance value and the stopping as required.

why doesnt it use the speed variable smiley-confuse

Code:
/*

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Pot to Analog 0
 * enable pin 8
 * motor 1 pin 9
 * motor 2 pin 10
 */

#include <LiquidCrystal.h>                          // include the library code:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);             // initialize the library with the numbers of the interface pins
 
        int sensorPin = A1;                         // select the input pin for the potentiometer for time
        int sensorValue = 0;                        // variable to store the value coming from the sensor

  const int motor1Pin = 9;                          // H-bridge leg 1 (pin 9, 1A)
  const int motor2Pin = 10;                         // H-bridge leg 2 (pin 10, 2A)
  const int enablePin =8;                           // H-bridge enable pin 8
  const int PotPin = A0;                            // input pin for the potentiometer for speed/direction
 
        int x = 10;
        int zero_speed;
        int Reverse;
        int Forward;
        int sensorValue2 = 1;                       // variable to store the value coming from the sensor
 
        int value = analogRead(PotPin);
        int speed = abs(value - 512)/2 ;            // 0..1023 -> -512.0..+512 -> 512..0..512 -> 256..0..256
        int direction = value < 512;                // or "value > 512" to reverse the action of the pot
 
        char flag;
       
        int countP = 0;
       
        int count1 = 0;
        int time_1;
        int speed1;
        int drive1;
       
 //*************************************************************************************
 
 void enter_point_a()                               // Subroutime "enter_point_a"
{
 
 
  lcd.clear();                                      // Clear Screen
  lcd.setCursor(1, 0);                              // set the cursor to column 0, line 0
  lcd.print("Enter Data for");                      // Print "Enter Data for"
  lcd.setCursor(4, 1);                              // set the cursor to column 4, line 1
  lcd.print("Point A");                             // Print "Point A"
  delay(3000);                                      // Delay
  lcd.clear();                                      // Clear Screen
 
  enter_data_1();
}
 //*************************************************************************************
void enter_data_1()                                   // Subroutime "enter_data_1"                 

  digitalWrite(7, HIGH);                            // set the LED on
 
  lcd.setCursor(0, 0);                              // set the cursor to column 0, line 0
  lcd.print("Time");                                // Print "Time"
  lcd.setCursor(4, 0);                              // set the cursor to column 4, line 0
  lcd.print(":");                                   // Print ":"
  lcd.setCursor(8, 0);                              // set the cursor to column 8, line 0
  lcd.print(".");                                   // Print ":"
  lcd.setCursor(12, 0);                             // set the cursor to column 12, line 0
  lcd.print("Min");                                 // Print "Min"
  lcd.setCursor(0, 1);                              // set the cursor to column 0, line 1
  lcd.print("Rotation");                            // Print "Rotation"
  lcd.setCursor(8,1);                               // set the cursor to column 8, line 0
  lcd.print(":");                                   //Print ":"
 

  Pot_Speed_Time_1();                                 // go to sub routine "Pot_Speed_Time"

  digitalWrite(7, LOW);                             // set the LED off
}

 //*************************************************************************************
   
  void Pot_Speed_Time_1()
  {
 
    while (digitalRead(1) == LOW)                 // if button is  not pressed
    {
    digitalWrite (enablePin, HIGH);
    int retval = analogRead(value);
     
      if (retval < 510)
        {
        Reverse = map(retval, 0, 510, 255, 0);
        analogWrite (motor1Pin, Reverse);
        count_pulse_neg_1();
        }
        else if(retval > 518)
          {
          Forward = map(retval, 518, 1023, 0, 255);
          analogWrite (motor2Pin, Forward);
          count_pulse_pos_1();
          }
        else
          {
          analogWrite (motor1Pin, 0);
          analogWrite (motor2Pin, 0);
          }

  int val = analogRead(sensorPin);            // read value from pot at sensorPin
      time_1 = map(val, 0, 1023, 0, 600);     // map function

    lcd.setCursor(6,0);                       // set the cursor to column 0, line 1
    lcd.print(time_1/60);                     // Print "val/100" 

    lcd.setCursor(8, 0);                      // set the cursor to column 2, line 1
    lcd.print(":");                           // Print ":"
 
    lcd.setCursor(9,0);                       // set the cursor to column 3, line 1
    lcd.print(time_1/10);                     // Print "val/10"
       
     
  if (digitalRead(1) == HIGH)                 // if button is pressed
    {                                         // then go to sub routine
    lcd.clear();                              // Clear Screen
 
    }
   }
 }

//***************************************************************************************** 

void count_pulse_pos_1()
{
  if((digitalRead(13) == HIGH) && (flag == 0))
    {
    count1 ++ ;
    flag = 1;
    }
   if(digitalRead(13) == LOW)
      {
      flag = 0;
      } 
      lcd.setCursor(10,1);   
      lcd.print((count1/1.4));

}

//*********************************************************************** 

void count_pulse_neg_1()

{
  if((digitalRead(13) == HIGH) && (flag == 0))
    {
    count1 --;
    flag = 1;
    }
   if(digitalRead(13) == LOW)
      {
      flag = 0;
      } 
      lcd.setCursor(10,1);   
      lcd.print((count1/1.4));
}

//***********************************************************************

void count_playbackF()

{
  if((digitalRead(13) == HIGH) && (flag == 0))
    {
    countP ++;
    flag = 1;
    }
   if(digitalRead(13) == LOW)
      {
      flag = 0;
      } 
}

//***********************************************************************

void count_playbackR()

{
  if((digitalRead(13) == HIGH) && (flag == 0))
    {
    countP --;
    flag = 1;
    }
   if(digitalRead(13) == LOW)
      {
      flag = 0;
      } 
}
//***********************************************************************

void zero_motor()
{
 zero_speed= map(x, 0,50,0, 255);
 lcd.setCursor(1,0);   
 lcd.print("Configuration");
 lcd.setCursor(2,1);   
 lcd.print("In Progress");
  while(1)
 {

   
     digitalWrite(enablePin, HIGH);
   
     analogWrite (motor1Pin, zero_speed); 
     digitalWrite(motor2Pin, LOW);


  if(digitalRead(6) == HIGH)
    {
     digitalWrite(motor1Pin, LOW); 
     digitalWrite(motor2Pin, LOW);
     lcd.clear();
     return;
    } 
  }
lcd.clear();
}

 //*************************************************************************************
 
void playback()
{

lcd.setCursor (4,0);
lcd.print("PLAYBACK");


while(1)
  {
   if (digitalRead(16) == HIGH) 
     {
     playbackA();
     }
  }
}
 //*************************************************************************************

void playbackA()
{
   
  while(1)
   {
    lcd.setCursor (4,1);
    lcd.print("POINT A");
   
    speed1 = count1/time_1;
   
    speed1 = abs(speed1);
    digitalWrite (enablePin, HIGH);
    drive1 = map(speed1, 0, speed1, 0, 225);
   
    if (count1 >(0))
      {
      analogWrite(motor1Pin, drive1);
      digitalWrite(motor2Pin, LOW);
      count_playbackF();
      }
    if (count1 <(0))
      {
      analogWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, drive1);
      count_playbackR();   
      }
    if (count1 == countP)
      {
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      break;
      }
   }

 }
//***********************************************************************

void setup()
{
lcd.begin(16, 2);       // set up the LCD's number of columns and rows:
zero_motor();

enter_point_a();        // go to sub routine "enter_point_a"
}

void loop()
{
playback();             // go to sub routine "enter_point_a_data" 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
    drive1 = map(speed1, 0, speed1, 0, 225);
drive1 will ALWAYS be 225, regardless of what value is in speed1.

What are you trying to do here?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well the Drive 1 function will be the PWM sent to the motor.

therefore i thought i would need the map the value for speed1 achieved using the equation:

speed1 = distance1 / time_1

to the digital output...

is this the correct way to do this??
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is this the correct way to do this?
I guess I wouldn't have asked what you were trying to do, if what you were doing was right.

The map function maps a value from one range (0 to speed1) to another range (0 to 225).

If the value being mapped is always the upper end of the from range, as in your case, the output will always be the upper end of the to range. Hence the map call is not doing anything useful. You might just as well have:
Code:
drive1 = 225;

So, again, the question is what range are you trying to map from, and what range are you trying to map to?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well what i have is time values of 0 - 600 seconds
and distance values of -1000 to +1000

however these vary depending on the required values but will be within the range...

therefore speed1 will be a variation of any combination between these values.

as speed = distance /time

for instance if:

distance = 200, time = 300, Speed1 will be  0.67

however the values will be different with different values for variables.

this is why i decided to map values from 0 to speed1...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for instance if:

distance = 200, time = 300, Speed1 will be  0.67
No, it won't. speed1 is an int, so 200/300 = 0.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh yes jus realised...

any ideas on hw i can solve the problem?
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use floating point variables?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if i change speed1 to a floating point variable will the code work as required...

i have found floating point constants in the reference material, is this the same??



Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if i change speed1 to a floating point variable will the code work as required...
No idea but without using floating point variables it will not work like you think it is.

Quote
i have found floating point constants in the reference material, is this the same??
No that is a constant, you want a variable.

http://www.arduino.cc/en/Reference/Float
Logged

Pages: 1 2 [3]   Go Up
Jump to: