Hi
I'm working on a project to produce an underwater scooter.
This includes two Maxon motors connected and controlled by force resistor sensors, this has been done successfully using an Elegoo Uno Rev 3 (Arduino Uno dupe) successfully.
In this I will also have a display screen (AZ Delivery OLED I2C Display Screen), this will show the value from a depth sensor (MS5400-CM) and the battery indicator.
Both of these set ups (Motor control and display screen) will run off the same Uno board.
I have two functioning codes for each:
Motor Control:
/* Simple example code for Force Sensitive Resistor (FSR) with Arduino. More info: https://www.makerguides.com */
// Define FSR pin:
#define fsrpin0 A0
int PWM_PIN0 = 6;
int PWM_PIN1 = 7;
int fsrreading0; //Variable to store FSR value
#define fsrpin1 A1
int fsrreading1; //Variable to store FSR value
//Define variable to store sensor readings:
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
pinMode(PWM_PIN0,OUTPUT);
pinMode(PWM_PIN1,OUTPUT);
}
void loop() {
// Read the FSR pin and store the output as fsrreading:
fsrreading0 = analogRead(fsrpin0);
if (fsrreading0 < 10) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 200) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 500) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 800) {
analogWrite(PWM_PIN0, fsrreading0);
}
delay(100); //Delay 500 ms.
fsrreading1 = analogRead(fsrpin1);
if (fsrreading1 < 10) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 200) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 500) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 800) {
analogWrite(PWM_PIN1, fsrreading0);
}
delay(100); //Delay 500 ms.
}
Display Screen:
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void setup(void) {
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,20,"Depth:"); // write something to the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,55 ,"insert"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(3000);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,20,"Battery:"); // write something to the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,55 ,"insert"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(3000);
}
However, when these have been merged together, it has a 4 second delay for the motor control. Previously this was instant! The display screen is also working as expected.
Merged Code:
/* Simple example code for Force Sensitive Resistor (FSR) with Arduino. More info: https://www.makerguides.com */
// Define FSR pin:
#define fsrpin0 A0
int PWM_PIN0 = 6;
int PWM_PIN1 = 7;
int fsrreading0; //Variable to store FSR value
#define fsrpin1 A1
int fsrreading1; //Variable to store FSR value
//Define variable to store sensor readings:
#include <U8g2lib.h>
// Include Wire Library for I2C
#include <Wire.h>
// Include Adafruit Graphics & OLED libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Include Adafruit AM2320 Temp Humid Library
// Reset pin not used but needed for library
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
pinMode(PWM_PIN0,OUTPUT);
pinMode(PWM_PIN1,OUTPUT);
u8g2.begin();
}
void loop() {
// Read the FSR pin and store the output as fsrreading:
fsrreading0 = analogRead(fsrpin0);
if (fsrreading0 < 10) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 200) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 500) {
analogWrite(PWM_PIN0, fsrreading0);
} else if (fsrreading0 < 800) {
analogWrite(PWM_PIN0, fsrreading0);
}
delay(100); //Delay 500 ms.
fsrreading1 = analogRead(fsrpin1);
if (fsrreading1 < 10) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 200) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 500) {
analogWrite(PWM_PIN1, fsrreading1);
} else if (fsrreading1 < 800) {
analogWrite(PWM_PIN1, fsrreading0);
}
delay(100); //Delay 500 ms
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,20,"Depth:"); // write something to the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,55 ,"insert"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(3000);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,20,"Battery:"); // write something to the internal memory
u8g2.setFont(u8g2_font_pxplusibmvga9_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,55 ,"insert"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(3000);
}
I was wondering if some could explain how I should be merging them to remove this (new) delay or if they can see a basic error that is making this incorrect.
Thank you !
Catherine