Arduino coding advise

Hi,

My son is doing A level engineering and is struggling with his project, sadly I am not very good with programming so I wondered if someone would be kind enough to help.

Thanks you very much see below.
David

I’m struggling with my code for my robotic lawnmower project. I am using an Arduino board and a perimeter wire sensor kit. I am trying to make the mower move up to a perimeter wire, stop, turn 90 degrees go forward slightly and turn 90 degrees again to simulate the motions of cutting grass. At the moment I have managed to get the mower to drive up to the wire and stop however I am having trouble making it turn 90 degrees. I am using two DC motors connected via an H bridge to an ardiuno mega 2560 board. Any help would be appreciated, I have copied the code below:


const int analogInPin = A0;  
const int analogOutPin = 9; 
const int ENA_PIN = 11; 
const int IN1_PIN = 7; 
const int IN2_PIN = 6;
const int ENB_PIN = 9;
const int IN3_PIN = 5;
const int IN4_PIN = 4;
int toggleSwitchPin1 = 2;
int relaypin = 3;

int toggleSwitchState1;

int sensorValue = 0;        
int outputValue = 0;        
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(ENB_PIN, OUTPUT);
  pinMode(IN3_PIN, OUTPUT);
  pinMode(IN4_PIN, OUTPUT);
  pinMode(toggleSwitchPin1, INPUT_PULLUP);
  pinMode(relaypin, OUTPUT);


}

void loop() {
  toggleSwitchState1 = digitalRead(toggleSwitchPin1);
  if (toggleSwitchState1 == HIGH) {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t out put = ");
  Serial.println(outputValue);
  delay(5);
  digitalWrite(relaypin, HIGH);
    while (outputValue < 100) {
      digitalWrite(IN1_PIN, HIGH);
      digitalWrite(IN2_PIN, LOW);
      digitalWrite(IN4_PIN, HIGH);
      digitalWrite(IN3_PIN, LOW);
      analogWrite(ENA_PIN, 180); 
      analogWrite(ENB_PIN, 180);
      delay(10);
      
    sensorValue = analogRead(analogInPin);
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    analogWrite(analogOutPin, outputValue);
  
    Serial.print("sensor = ");
    Serial.print(sensorValue);
    Serial.print("\t out put = ");
    Serial.println(outputValue);
    delay(5);
    //digitalWrite(IN1_PIN, LOW);
    //digitalWrite(IN2_PIN, LOW);
    //digitalWrite(IN4_PIN, LOW);
    //digitalWrite(IN3_PIN, LOW);
    }
   analogWrite(ENA_PIN, 100);
    analogWrite(ENB_PIN, 100);  
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, HIGH);
    digitalWrite(IN3_PIN, LOW);
    digitalWrite(IN4_PIN, HIGH);
    analogWrite(ENA_PIN, 100);
    analogWrite(ENB_PIN, 100);
   delay(2000);
    digitalWrite(IN1_PIN, HIGH);
    digitalWrite(IN2_PIN, LOW);
    digitalWrite(IN3_PIN, HIGH);
    digitalWrite(IN4_PIN, LOW);
    delay(2000);
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, HIGH);
    digitalWrite(IN3_PIN, LOW);
    digitalWrite(IN4_PIN, HIGH);
    delay(2000);
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, LOW);
    digitalWrite(IN3_PIN, LOW);
    digitalWrite(IN4_PIN, LOW);
    }
1 Like

Hello @dcrane7,

Happy to hear that although you don’t have much programming knowledge you are still trying to help your son!

Even though it would be easier simply to tell you how to modify the code to make it work I think it would be best if you, and your son, learn what’s behind it.

I think this tutorial does a great job explaining how an H-bridge works, and what’s the meaning of each input:

The code shared there is a bit more complex than what your son wants to achieve as it allows to not only turn left or right but also control the speed of the wheels, which allows turning at different angles (not only 90 degrees).

However, if you simply want to achieve turning 90 deg, you just need to understand when to use HIGH or LOW for the inputs, keeping in mind that to be able to turn the motors should be rotating in opposite directions.

I hope that helps!

1 Like