Firstly I will post the code at hand. Excuse the poorly written and copy pasted code I've moved around trying to find the issue I'm new to this....
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Servo ESC; // create servo object to control the ESC
int potValue; // value from the analog pin
int convert;
int percentvalue;
int potRead;
#define OLED_RESET 4 //Digital pin 4 set aside for OLED reset
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //start the oled called display with a a hex addy of 0x3c
display.display(); //Show what's in the buffer
delay(1000); //take a breath
display.clearDisplay(); //clear the screen
// Attach the ESC on pin 9
}
void loop() {
potValue = analogRead(A0); //read the voltage on pin A0 and put the ADC value into variable level
potRead = map(potValue, 0, 1023, 0, 180); // scale it to use it with the servo library (value between 0 and 180)
ESC.write(potRead); // Send the signal to the ESC
convert = map(potValue, 140, 1023, 0, 128); //convert the 0-1024 raw ADC to a more OLED friendly 1-128
percentvalue = map(potValue, 140, 1023, 0, 100); //convert the 0-1024 raw ADC to a more OLED friendly 1-128
static const unsigned char PROGMEM image_Layer_4_bits[] = {0x70,0x00,0x88,0x20,0x88,0x40,0x88,0x80,0x71,0x00,0x02,0x00,0x04,0xe0,0x09,0x10,0x11,0x10,0x21,0x10,0x40,0xe0};
static const unsigned char PROGMEM image_Layer_3_bits[] = {0x07,0xff,0xfc,0x08,0x00,0x02,0x08,0x00,0x02,0x78,0x00,0x02,0xf8,0x00,0x02,0xf8,0x00,0x02,0xf8,0x00,0x02,0xf8,0x00,0x02,0x78,0x00,0x02,0x08,0x00,0x02,0x08,0x00,0x02,0x07,0xff,0xfc};
if (percentvalue <= 0){
display.clearDisplay();
display.drawBitmap(105, 0, image_Layer_3_bits, 23, 12, 1);
display.drawBitmap(53, 2, image_Layer_4_bits, 12, 11, 1);
display.setTextSize(2); //set up text size
display.setTextColor(WHITE); //set text color
display.setCursor(40,0); //where to position cursor (128,64)
display.fillRect(1, 20, 0, 20, WHITE); //Draw a rectangle (x,y,width,height,color)
//display.clearDisplay(); //another flush
display.println(0); //add data in the buffer - the variable level
display.display();
}
else if ((percentvalue <= 9) && (percentvalue >= 0)){
display.clearDisplay();
display.drawBitmap(105, 0, image_Layer_3_bits, 23, 12, 1);
display.drawBitmap(53, 2, image_Layer_4_bits, 12, 11, 1);
display.setTextSize(2); //set up text size
display.setTextColor(WHITE); //set text color
display.setCursor(40,0); //where to position cursor (128,64)
display.fillRect(1, 20, convert, 20, WHITE); //Draw a rectangle (x,y,width,height,color)
//display.clearDisplay(); //another flush
display.println(percentvalue); //add data in the buffer - the variable level
display.display(); //show the buffer
}
else if ((percentvalue >= 10) && (percentvalue <= 99)){
display.clearDisplay();
display.drawBitmap(105, 0, image_Layer_3_bits, 23, 12, 1);
display.drawBitmap(65, 2, image_Layer_4_bits, 12, 11, 1);
display.setTextSize(2); //set up text size
display.setTextColor(WHITE); //set text color
display.setCursor(40,0); //where to position cursor (128,64)
display.fillRect(1, 20, convert, 20, WHITE); //Draw a rectangle (x,y,width,height,color)
//display.clearDisplay(); //another flush
display.println(percentvalue); //add data in the buffer - the variable level
display.display();
}
else if (percentvalue >= 100){
display.clearDisplay();
display.drawBitmap(105, 0, image_Layer_3_bits, 23, 12, 1);
display.drawBitmap(76, 2, image_Layer_4_bits, 12, 11, 1);
display.setTextSize(2); //set up text size
display.setTextColor(WHITE); //set text color
display.setCursor(40,0); //where to position cursor (128,64)
display.fillRect(1, 20, convert, 20, WHITE); //Draw a rectangle (x,y,width,height,color)
//display.clearDisplay(); //another flush
display.println(percentvalue); //add data in the buffer - the variable level
display.display();
}
}
My issue is that with this code my motor will fluctuate speed just slightly, up and down.
I'm not sure if this is happening due to my mappings or if its the rate at which the code executes.
If I run just a very basic ESC code I do not get the issue. If I comment out all but the first if statement it also seems to run without these fluctuations.
Any info would be appreciated.. Its late and I have been at it for hours beating my head against the breadboard...