Hallo Forum,
i have started a Projekt with an Arduino UNO / Nano to control a Servo and 2 Neopixel Rings (24) with Remote Control.
I am new in Arduino Programming and need some help with a usefull and working programm structur.
Here are my code:
#include <IRremote.h>
#include <Servo.h>
#define plus 16748655 //IR Code für Taste
#define minus 16754775 //IR Code für Taste
#define stops 16736925 //IR Code für Taste
#define tata 16728765
int RECV_PIN = 2; //IR receiver pin
Servo servo;
int val = 90; // bei 360 Grad Servo Stop Position
int button1_PIN = 4; // pin für Button1
int button2_PIN = 7; // pin für Button2
int button1_STATE = 0; // Button1 gedrückt Status
int button2_STATE = 0; // Button2 gedrückt Status
int button_PRESSED = 0; // Status Button 1=oben, 2=unten, 0=neutral
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servo.attach(9); // pin für Servo
pinMode(button1_PIN, INPUT_PULLUP);
pinMode(button2_PIN, INPUT_PULLUP);
}
void loop()
{
button1_STATE = digitalRead(button1_PIN);
button2_STATE = digitalRead(button2_PIN);
Serial.println(button1_STATE);
Serial.println(button2_STATE);
Serial.println(button_PRESSED);
Serial.println(val);
if (button1_STATE == HIGH && button2_STATE == HIGH && button_PRESSED != 0) // kein Button gedrückt von Status 1 oder 2
{
button_PRESSED = 0; // Button Status auf neutral 0
irrecv.resume(); // Receive the next value
}
if (button1_STATE == LOW && button_PRESSED == 0) // Button oben gedrückt von Status neutral
{
irrecv.resume();
button_PRESSED = 1; // Button Status auf 1
val = 90;
// Servo aus
// Receive the next value
}
if (button2_STATE == LOW && button_PRESSED == 0) // Button unten gedrückt von Status neutral
{
button_PRESSED = 2; // Button Status auf 2
val = 90; // Servo aus
irrecv.resume(); // Receive the next value
}
if (irrecv.decode(&results) && button_PRESSED == 1 ) // Wenn RC und Button oben gedrückt -> nur Richtung runter
{
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
if (results.value == minus) // IR Taste minus
{ val = 20; } // gedrückt -> Servo lauf runter
else
{ val = 90; } // gehalten -> Servo 1 Step runter
}
if (irrecv.decode(&results) && button_PRESSED == 2 ) // Wenn RC und Button unten gedrückt -> nur Richtung rauf
{
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
if (results.value == plus) // IR Taste plus
{ val = 160;} // gedrückt -> Servo lauf rauf
else
{ val = 90; } // gehalten -> Servo 1 Step rauf
}
if (irrecv.decode(&results) && button_PRESSED == 0 ) // Wenn RC und Status neutral -> alle Richtungen
{
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
if (results.value == plus) // IR Taste plus
{ val = 160;} // gedrückt -> Servo lauf rauf
else if (results.value == minus) // IR Taste minus
{ val = 20;}
else if (results.value == tata)
{
val = 90;
}
}
servo.write(val);
delay(200); // General speed
}
Part 1
Every is working so far.
when i press + on the remote the servo go in direction up till button_1 is low then the servo stop an it also is only possible to press -
when i pres - on the remote the servo go in direction down till button_2 is low thej the servo stop and it is only possible to press +
at the moment everything is in the loop function
Part 2
I want to include 2 NEOPIXEL RINGS RGBW 24 and there should be no problem getting the information form the remote (irrecv.resume()![]()
Also i should be possible to start different themes of the NEOPIXEL RINGS.
My Question:
What is the best structur for the programm?
Should i make several Functions and call them in the loop and how could i have a perfect timing
between remote, servosteps and NEOPIXEL performance.
THX




