Motor controlled by temperature

Hi I’m new here and looking for some help ! I’m currently working on a project of my own . It’s a heat following robot . It has two TMP36 temperature sensors (labelled temperature and temperature1 in the arduino code) connected to the arduino nano . And also connected is the L298N motor controller . I have a code already which converts the voltage from the sensors and displays a temperature, and in there I have put some basic code for the movement of the wheels from the motor . Unfortunately I’m very new to arduino , and I don’t get the syntax. All I need is to tweak my code to make the robot move forward and right if temperature>temperature1 and the other way if temperature1>temperature . I also need the robot to reverse if either temperature or temperature1 are above 40 degrees. I Have copy pasted my code below in case anyone can help me :slightly_smiling_face: (ignore the comments I’m using them to learn lol)

#define sensorPin A0
#define sensorPin1 A1

// Motor A connections
//int enA = 9;
int in1 = 3;
int in2 = 4;
// Motor B connections
//int enB = 3;
int in3 = 5;
int in4 = 6;

 
 void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Set all the motor control pins to outputs
  //pinMode(enA, OUTPUT);
  //pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
 
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop() {
  directionControl();
  delay(1000);
  speedControl();
  delay(1000);

  // Get a reading from the temperature sensor:
  int reading = analogRead(sensorPin);
int reading1 = analogRead(sensorPin1);

  // Convert the reading into voltage:
  float voltage = reading * (5000 / 1024.0);
float voltage1 = reading1 * (5000 / 1024.0);
  // Convert the voltage into the temperature in Celsius:
  float temperature = (voltage - 500) / 10;
float temperature1 = (voltage1 - 500) / 10;
  // Print the temperature in the Serial Monitor:
  Serial.print(temperature);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("C");
 Serial.print(temperature1);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("C");
  delay(1000); // wait a second between readings
}


// This function lets you control spinning direction of motors
void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  //analogWrite(enA, 255);
  //analogWrite(enB, 255);

  // Turn on motor A & B

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
 
  // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
 
  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

// This function lets you control speed of the motors
void speedControl() {
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
 
  // Accelerate from zero to maximum speed
  //for (int i = 0; i < 256; i++) {
    //analogWrite(enA, i);
    //analogWrite(enB, i);
    //delay(20);
  //}
 
  // Decelerate from maximum speed to zero
  //for (int i = 255; i >= 0; --i) {
    //analogWrite(enA, i);
    //analogWrite(enB, i);
    //delay(20);
 //
}

add 'code' tags like this int x = 4

Please edit your code to add code tags ("</>" editor button).

The first thing to do is make sure that you can control the robot, using simple code.

The second thing to do is to learn how to use the temperature sensors, also with simple code.

When that is all working to your satisfaction working, then start merging the two programs together.

No need to convert to temperature.
Just use the analogue values.

Simplified code...

if (analogRead(A0) < analogRead(A1))  // go right
else if (analogRead(A0) > analogRead(A1))  // go right
else   // go straight

Leo..

Remove all delay from your code.
Now it will only read the sensors every 3 seconds.
That leaves ample time to loose track...
Why do you turn on the motor in controlSpeed?
You should make 3 functions . MoveForward, turnRight and turnLeft.
And then add some logic to call these functions vased on the readings.
Why not name your variables leftTreading and rightTreading?

Likely 4. Reverse is also wanted as an option.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.