Advice on making 2 values into one?? and question on PWM value

Im new to arduino programing and Im building an Object avoiding robot with a sharp IR sensor and im running into some problems when im trying to program it.

so far I've got the robot to move forward fuldspeed - when the IR sensor detects something, the robot goes fullspeed backwards. pretty simple. Now I want it to turn around when it detects an object by turning one motor forward and the other motor backward in half speed mode (128/255) with a delay time on 1000ms and a break on 2000ms (just to show off) and then moving forward again until detecting something else.

my code which goes forward fullspeed and backwards fullspeed:

  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int motor3Pin = 5;    // H-bridge leg 3 (pin 10,3A)
  const int motor4Pin = 7;    // H-bridge leg 4 (pin 15,4A)
  const int enable1Pin = 9;    // H-bridge enable pin
  const int enable2Pin = 10;   // H-bridge enable pin
  const int ledPin = 13;      // LED 
  const int STATE = 2;   // set pin 2 for sensor reading
  int r_state = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor
void setup()
  {

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin,  OUTPUT); 
    pinMode(motor2Pin,  OUTPUT); 
    pinMode(motor3Pin,  OUTPUT);
    pinMode(motor4Pin,  OUTPUT);
    pinMode(enable1Pin, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (STATE, INPUT);  // sets pin 2 as digital input 

    // set enablePin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH); 
    digitalWrite(enable2Pin, HIGH);
  }

void loop(){
  r_state = digitalRead(STATE);    // reads the status of the sensor
  if(r_state == 0) {
  digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
  digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  digitalWrite(motor3Pin, LOW);
  digitalWrite(motor4Pin, HIGH);
} 
   else   {       // if the switch is low, motor will turn in the other direction:

      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      digitalWrite(motor3Pin, HIGH);
      digitalWrite(motor4Pin, LOW);
    
    }
  }

and then I've tried to figure out how to change it so the robot operates the way I want it to. I've commented the places I wanted to write something:

So what I wanted is that digitalWrite(motor1Pin, LOW); // digitalWrite(motor2Pin, HIGH); // motor1Pin and motor2Pin becomes one unit and get named rightmotor and I don't know how to code that.

Then I wanted to give that name the value of 128 so that right motor will move forward at halfspeed

Then I wanted to change the LOW and HIGH around motor3pin and motor4pin so the left motor would turn backwards and also bring them together to one unit/name (leftmotor) and give leftmotor the value of 128 so i would turn around for 1 sek so the delay would be 1000ms

digitalWrite(motor3Pin, HIGH); digitalWrite(motor4Pin, LOW);

Any one who can help me? I've search google through and this forum before I posted. Sorry for my english.

Best regards

Dynamitetalks: So what I wanted is that digitalWrite(motor1Pin, LOW); // digitalWrite(motor2Pin, HIGH); // motor1Pin and motor2Pin becomes one unit and get named rightmotor and I don't know how to code that.

Perhaps you want to write a function to control the right motor:

void RightMotorForward(int speed) {
  digitalWrite(motor1Pin, LOW);
  analogWrite(motor2Pin, speed);
}

Similarly you could write RightMotorBackward(), LeftMotorForward(), and LeftMotorBackward().

Thanks for your reply Johnwasser.

So I've tried to put the code you wrote below the void loop() and got an error..

Sharp_hbro_motor2_autonomous1.ino: In function 'void loop()':
Sharp_hbro_motor2_autonomous1:34: error: expected primary-expression before 'int'
Sharp_hbro_motor2_autonomous1:34: error: 'RightMotorForward' was not declared in this scope
Sharp_hbro_motor2_autonomous1:35: error: expected `;' before 'digitalWrite'
Sharp_hbro_motor2_autonomous1:36: error: 'speed' was not declared in this scope

I've tried to change this "{"symbol around and also tried to insert ";" after RightMotorForward(int speed) but it didn't change anything. so I have a hard time figuring out how to solve this problem so no errors will occur..

Im thinking the problem is that RightMotorForward haven't been registred before the statement or is it int speed which haven't been registred before?

thanks again and best regards

So I've tried to put the code you wrote below the void loop() and got an error

Show us!

 const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int motor3Pin = 5;    // H-bridge leg 3 (pin 10,3A)
  const int motor4Pin = 7;    // H-bridge leg 4 (pin 15,4A)
  const int enable1Pin = 9;    // H-bridge enable pin
  const int enable2Pin = 10;   // H-bridge enable pin
  const int ledPin = 13;      // LED 
  const int STATE = 2;   // set pin 2 for sensor reading
  int r_state = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor
void setup()
  {

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin,  OUTPUT); 
    pinMode(motor2Pin,  OUTPUT); 
    pinMode(motor3Pin,  OUTPUT);
    pinMode(motor4Pin,  OUTPUT);
    pinMode(enable1Pin, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (STATE, INPUT);  // sets pin 2 as digital input 

    // set enablePin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH); 
    digitalWrite(enable2Pin, HIGH);
  }

void loop(){
  r_state = digitalRead(STATE);    // reads the status of the sensor
  if(r_state == 0) {
  //digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
  //digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
    RightMotorForward(int speed)
  digitalWrite(motor1Pin, LOW);
  analogWrite(motor2Pin, speed);

}
  //digitalWrite(motor3Pin, HIGH);  //bytter lige rundt på low og high her
 // digitalWrite(motor4Pin, LOW);
  

   else   {       // if the switch is low, motor will turn in the other direction:

      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      digitalWrite(motor3Pin, HIGH);
      digitalWrite(motor4Pin, LOW);
    
    }
  }

Putting every { on a new line would help. Using Tools + Auto Format would help.

    RightMotorForward(int speed)

Is this supposed to be a function declaration? If so, it's in the wrong place (belongs outside a function) and needs a return type.

If it's supposed to be a function call, the ; on the end is missing, and speed is not a defined variable.

Thanks PoulS, that was a great comment

Im sorry I've a hard time expressing myself, What I want is:

A name for each motor - Which can get a PWM value from 0 - 255

since I have motor1Pin and motor2Pin as the right motor in my robot, I want both of the name to be combine to one name I can write to and define the speed for that motor.

Im really bad in programming, so its a bit difficult, to understand the big picture of all the terms and names..

however if I want speed to be a defined variable of a PWM signal at the value of 128, how is that written? something like this?: int speed = 128; or how do I do it?

Best regards

since I have motor1Pin and motor2Pin as the right motor in my robot, I want both of the name to be combine to one name I can write to and define the speed for that motor.

As John was showing you, you need to create a function that you can call. He showed what the function should look like. Then, you need to call the function:

   int speed = 14;
   RightMotorForward(speed);

The declaration/definition of the function goes AFTER loop(). The call to the function goes IN loop().

Where and how is this supposed to be inserted to the code, because so far I only got errors from all my attempts..

Why did you write int speed = 14; what does the 14 mean or do?

When after loop is it? before the IF statement or after? or after the Else statement?

aint int speed=14; meant to be inserted before the void setup() ?

Dynamitetalks: Where and how is this supposed to be inserted to the code

As PaulS said already.

Why did you write int speed = 14; what does the 14 mean or do?

As the variable name suggests, 14 is the speed.

When after loop is it? before the IF statement or after? or after the Else statement?

Neither. There are no if/else statements after the loop:

void loop()
{
  // code in loop
}

// code after loop

aint int speed=14; meant to be inserted before the void setup() ?

What he posted was example. Your code will determine what gets passed to the function.

Perhaps something like this would be good to read:

http://www.cplusplus.com/doc/tutorial/functions/

Thanks alot Arrch, I'll try to understand it.

Best regards

I barely get it… ære there someone who would insert the code the right places so it will compile, maybe i will learn it, atleast i will her some directions.

Best regards

Dynamitetalks: I barely get it.. ære there someone who would insert the code the right places so it will compile, maybe i will learn it, atleast i will her some directions.

Best regards

Or better yet, post what you have so we can correct your thought process, allowing you to learn why and not just how.

Thanks Arrch

Can I make any function just by typing: Void() ? and how do I "call" that function afterwards?

  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int motor3Pin = 5;    // H-bridge leg 3 (pin 10,3A)
  const int motor4Pin = 7;    // H-bridge leg 4 (pin 15,4A)
  const int enable1Pin = 9;    // H-bridge enable pin
  const int enable2Pin = 10;   // H-bridge enable pin
  const int ledPin = 13;      // LED 
  const int STATE = 2;   // set pin 2 for sensor reading
  int r_state = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor
  int speed = 14;      // HERE I HAVE CHANGED SOMETHING
  void setup()
  {

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin,  OUTPUT); 
    pinMode(motor2Pin,  OUTPUT); 
    pinMode(motor3Pin,  OUTPUT);
    pinMode(motor4Pin,  OUTPUT);
    pinMode(enable1Pin, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (STATE, INPUT);  // sets pin 2 as digital input 
    
    // set enablePin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH); 
    digitalWrite(enable2Pin, HIGH);
  }

void loop()
  
  
  void RightMotorForward(int speed)// HERE I HAVE CHANGED SOMETHING
  { 
  digitalWrite(motor1Pin, LOW);// HERE I HAVE CHANGED SOMETHING
  analogWrite(motor2Pin, speed);// HERE I HAVE CHANGED SOMETHING
  }
    
  

  {
    
  r_state = digitalRead(STATE);    // reads the status of the sensor
  if(r_state == 0) {
  //digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
  //digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
   
// WHAT SHOULD I TYPE HERE, BECAUSE AS I UNDERSTANDS BY YOU ALL
// EVERYTHING I SHOULD TYPE AINT SUPPOSED TO BE HERE, WHICH I DONT GET..
}
  //digitalWrite(motor3Pin, HIGH);  //bytter lige rundt på low og high her
 // digitalWrite(motor4Pin, LOW);



   else  
  
   {       // if the switch is low, motor will turn in the other direction:

      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      digitalWrite(motor3Pin, HIGH);
      digitalWrite(motor4Pin, LOW);
    
    }
  }

Can I make any function just by typing: Void() ?

No, and you can't embed one function in another.

Get a book on C programming. Work through that. When you get to the end, come on back.

Look at the difference between between the curly braces here:

void loop()
  
  void RightMotorForward(int speed)// HERE I HAVE CHANGED SOMETHING
  { 
    // code here
  }

  // code here
}

and here:

void loop()
{
  // code here
}

void someFunction()
{
  // code here
}

how do I "call" that function afterwards

void loop()
{
  someFunction(); // call the function
  // code here
}

void someFunction()
{
  // code here
}

Thanks for the help everybody,

Now I've got it down to one error at line 33, saying: expected initializer before 'void'

I can't figure out the problem, my code has ended up like this:

  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int motor3Pin = 5;    // H-bridge leg 3 (pin 10,3A)
  const int motor4Pin = 7;    // H-bridge leg 4 (pin 15,4A)
  const int enable1Pin = 9;    // H-bridge enable pin
  const int enable2Pin = 10;   // H-bridge enable pin
  const int ledPin = 13;      // LED 
  const int STATE = 2;   // set pin 2 for sensor reading
  int r_state = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor
  int speed = 14;      // 
  void setup()
  {

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin,  OUTPUT); 
    pinMode(motor2Pin,  OUTPUT); 
    pinMode(motor3Pin,  OUTPUT);
    pinMode(motor4Pin,  OUTPUT);
    pinMode(enable1Pin, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (STATE, INPUT);  // sets pin 2 as digital input 
    
    // set enablePin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH); 
    digitalWrite(enable2Pin, HIGH);
  }

void loop()
  
  
 void RightMotorForward(int speed) // HERE IS LINE 33
 {
  digitalWrite(motor1Pin, LOW);
  analogWrite(motor2Pin, speed);
  {
  void LeftMotorForward(int speed)
  {
  digitalWrite(motor3pin, HIGH);
  analogWrite(motor4pin, speed);
  }
  }
 }  
  r_state = digitalRead(STATE);    // reads the status of the sensor
  if(r_state == 0) 
  //digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
  //digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  void RightMotorForward(){
 speed=14; 

  }


  //digitalWrite(motor3Pin, HIGH);  
 // digitalWrite(motor4Pin, LOW);



   else  
  
   {       // if the switch is low, motor will turn in the other direction:

      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      digitalWrite(motor3Pin, HIGH);
      digitalWrite(motor4Pin, LOW);
    
    }
  }

Would someone please help point out or change the code so its compiles, I've really spends alot of hours so far, and would love to build my robot :)

Best regards

Dynamitetalks: Would someone please help point out or change the code so its compiles, I've really spends alot of hours so far, and would love to build my robot :)

This code compiles:

const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int motor3Pin = 5;    // H-bridge leg 3 (pin 10,3A)
const int motor4Pin = 7;    // H-bridge leg 4 (pin 15,4A)
const int enable1Pin = 9;    // H-bridge enable pin
const int enable2Pin = 10;   // H-bridge enable pin
const int ledPin = 13;      // LED 
const int STATE = 2;   // set pin 2 for sensor reading
int r_state = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor

void setup() {
  // set all the other pins you're using as outputs:
  pinMode(motor1Pin,  OUTPUT); 
  pinMode(motor2Pin,  OUTPUT); 
  pinMode(motor3Pin,  OUTPUT);
  pinMode(motor4Pin,  OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  pinMode(enable2Pin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode (STATE, INPUT);  // sets pin 2 as digital input 

  // set enablePin high so that motor can turn on:
  digitalWrite(enable1Pin, HIGH); 
  digitalWrite(enable2Pin, HIGH);
}

void loop() {
  r_state = digitalRead(STATE);    // reads the status of the sensor
  if(r_state == 0) {
    //digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    //digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high

    //digitalWrite(motor3Pin, HIGH);  
    // digitalWrite(motor4Pin, LOW);


  }
  else {       // if the switch is low, motor will turn in the other direction:
    digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    digitalWrite(motor3Pin, HIGH);
    digitalWrite(motor4Pin, LOW);
  }
}


void RightMotorForward(int speed) {
  digitalWrite(motor1Pin, LOW);
  analogWrite(motor2Pin, speed);
}

void LeftMotorForward(int speed) {
  digitalWrite(motor3Pin, HIGH);
  analogWrite(motor4Pin, speed);
}

This code compiles:

You're the man, Thanks! but what is the speed when the IR sensor detects something?

Dynamitetalks:

This code compiles:

You're the man, Thanks! but what is the speed when the IR sensor detects something?

If by "detects something" you mean r_state == 0 then nothing happens. All that code is commented out. If you want it to move forward I would put in calls to RightMotorForward() and LeftMotorForward(). When you add those calls you can specify whatever speed you want.

You asked for code that would compile, not code that would do what you intended.

For custom programming I get $20/hour (one hour minimum). :)