Hello, im very new to this so im sorry if my question lacks information or isnt properly explained.
I have recently changed the coding for my project that includes a servo. After having done this tho, the servo now twitches. it has enough Voltage and Amperage wich i thought would have been the problem but its not.
I gues the coding i added for the display to read out the temp sensor might conflict with the library of the servo, but unfortunatly i do no understand enough to know this.
If any of u who do understand this could look at my code to see if this is the case i would appreciate it very much.
thanks!
#include <Servo.h>
#include <IRremote.h>
Servo servo_5;
const int irReceiverPin = 3; // Connect the IR receiver sensor to pin 2
// Connect an LED to pin 13 or use a relay for controlling a device
IRrecv irReceiver(irReceiverPin);
decode_results results;
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 6
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 6
Adafruit_SSD1306 display(OLED_RESET);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
irReceiver.enableIRIn(); // Start the IR receiver
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
servo_5.attach(5, 500, 2500);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
servo_5.write(60);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
sensors.begin(); // initialize the DS18B20 temperature sensor:
}
void loop() {
if (irReceiver.decode(&results)) {
Serial.println(results.value, HEX);
// Check for the specific IR codes
if (results.value == 0x9716BE3F) { // Change this value with the actual IR code
activateTable();
}
else if (results.value == 0x3D9AE3F7) {
activateSecondFeature();// Add code to handle the second IR command
// For example: activateSecondFeature();
}
else if (results.value == 0x6182021B) {
activateThirdFeature();// Add code to handle the third IR command
// For example: activateThirdFeature();
}
else if (results.value == 0x8C22657B) {
activateFourthFeature();// Add code to handle the third IR command
// For example: activateThirdFeature();
}
else if (results.value == 0x488F3CBB) {
activateFithFeature();// Add code to handle the third IR command
// For example: activateThirdFeature();
}
else if (results.value == 0x449E79F) {
activateSixthFeature();// Add code to handle the third IR command
// For example: activateThirdFeature();
}
// Add more conditions for additional IR commands as needed
irReceiver.resume(); // Receive the next value
}
{
display.clearDisplay();
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(sensors.getTempCByIndex(0)); // get and print the temperature in degree Celsius
delay(1000);
float T = sensors.getTempCByIndex(0); // let T be temperature in degC from sensor
// floating-point number, with a decimal point
display.setTextSize(3);display.setTextColor(WHITE); display.setCursor(0,0);
display.print(T);
display.cp437(true); // code page 437
display.write(167); // character 167 is degree
display.println("C");
display.display(); // for the changes to make effect
}
}
void activateTable() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7, HIGH);
delay(22000); // Wait for 3000 millisecond(s)
digitalWrite(7, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
// Add functions for handling other features as needed
// For example:
void activateSecondFeature() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000);// Wait for 1000 millisecond(s)
digitalWrite(8, HIGH);
delay(22000); // Wait for 3000 millisecond(s)
digitalWrite(8, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
void activateThirdFeature() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000);// Wait for 1000 millisecond(s)
digitalWrite(12, HIGH);
delay(62000); // Wait for 3000 millisecond(s)
digitalWrite(12, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
void activateFourthFeature() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000);// Wait for 1000 millisecond(s)
digitalWrite(13, HIGH);
delay(62000); // Wait for 3000 millisecond(s)
digitalWrite(13, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
void activateFithFeature() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000);// Wait for 1000 millisecond(s)
digitalWrite(12, HIGH);
digitalWrite(8, HIGH);
delay(20000); // Wait for 3000 millisecond(s)
digitalWrite(8, LOW);
delay(40000);
digitalWrite(12, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
void activateSixthFeature() {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(6000); // Wait for 5000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(110);
delay(1000);// Wait for 1000 millisecond(s)
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
delay(20000); // Wait for 3000 millisecond(s)
digitalWrite(7, LOW);
delay(40000);
digitalWrite(12, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_5.write(60);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}