Good afternoon everybody,
being new to the Arduino world (1 week of experience), mainly in programming it, I come to ask for help.
I have in mind to use an Arduino Nano to control a conveyor belt powered by a simple DC MOTOR and two sensors to control the position of the belt. The electrical and electronic part I already did and is working. The part of the program is that something is missing. The idea is that the Arduino controls the engine through two Relays that make the engine inversion and reaching the right or left position stops the engine through one of the sensors. What I cannot put into the code is that the engine reaching one of the two positions is stopped for a period of time before starting again.
Here is the code I am using
//////////////////////////////////////////////////////////
const int SENSOR1 = 2; //
const int SENSOR2 = 3; //
const int RELAY1 = 6; //
const int RELAY2 = 7; //
int SENSORstate1 = 0; //
int SENSORstate2 = 0; //
//////////////////////////////////////////////////////////
void setup() {
pinMode(RELAY1,OUTPUT); pinMode(RELAY2,OUTPUT);
pinMode(SENSOR1,INPUT); pinMode(SENSOR1,INPUT);
}
//////////////////////////////////////////////////////////
void loop() {
SENSORstate1 = digitalRead(SENSOR1);
if (SENSORstate1 == HIGH){
//delay(2000);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY1,LOW);
//delay(2000);
}
SENSORstate2 = digitalRead(SENSOR2);
if (SENSORstate2 == HIGH){
//delay(2000);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY1,HIGH);
//delay(2000);
}
}
//////////////////////////////////////////////////////////
Until now there is no Start/ Stop button ..... :). But YES it must to have it. Right now with the waiting time doubt that i have, all this would also not properly work. For example without start button all this would begun only to move if i would manually activate one of the sensor´s. But this i think i can solve if i put digitalWrite(RELAY2,HIGH); and digitalWrite(RELAY1,LOW);" in the setup. What it´s i think not the best way. The best way woul be to continuo from where it eas before the stop. I will come to this point later in my learning process.
Yes i know that i can make all this without a arduino. Simply two relay´s , the sensor´s , start/stop button...etc. electrically connected and it would work. But it´s how i decided to enter and begun to learn the arduio world.
I use the delay() function, but for sure on a wrong way in the wrong place by the reason of my wrong logical thinking. Take a look on the code that i have. There is the delay() function. Only like this when the conveyor reach one of the sensor´s he will stop only after the 6sec and inverse the rotation after again other 6sec. I trie this out on the equiment.
//////////////////////////////////////////////////////////
const int SENSOR1 = 2; //
const int SENSOR2 = 3; //
const int RELAY1 = 6; //
const int RELAY2 = 7; //
int SENSORstate1 = 0; //
int SENSORstate2 = 0; //
//////////////////////////////////////////////////////////
void setup() {
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(SENSOR1,INPUT);
pinMode(SENSOR1,INPUT);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY1,LOW);
}
//////////////////////////////////////////////////////////
void loop() {
SENSORstate1 = digitalRead(SENSOR1);
if (SENSORstate1 == HIGH){
digitalWrite(RELAY1,LOW);
delay(6000);
digitalWrite(RELAY2,HIGH);
}
SENSORstate2 = digitalRead(SENSOR2);
if (SENSORstate2 == HIGH){
digitalWrite(RELAY2, LOW);
delay(6000);
digitalWrite(RELAY1,HIGH);
}
}
The idea is to stop immediately in inverse the rotaion after 6 sec.
Hi ,
I found some time again this evening to deal with my conveyor belt program.
Made me read through some ideas that I didn't have. I saw the delay () function in the same way as time functions that are used in PLC programming. Didn't know the delay () stoped the complet prog for the time. The function millis () however does not stop the prog. Tried to disassemble the task as best I could in order to have a better overview. Certainly not the best disassemble, as C ++ is completely new to me. A switch to turn off is already inside :). Switching on still fails:
Back to millis (). I added it to the program .... Nothing happens?!?!?! One of the sensors becomes active, the motor changes direction wonderfully and the waiting time was not given.
I noticed that the PLC way of thinking I can't use it. >:( :o
---- | | ------ | | ---------- ()
T1 I0.1 RQ1
Will show the code that I have until now. If someone could have a quick look and give his opinion, I would be thankful.
const int sensor1 = 2;
const int sensor2 = 3;
const int start1 = 4;
const int stop1 = 5;
const int relais1 = 6;
const int relais2 = 7;
int relais1State = LOW;
int relais2State = LOW;
int sensor1State = 0;
int sensor2State = 0;
int start1State = 0;
int stop1State = 0;
unsigned long previousMillisrecht = 0;
unsigned long previousMillislinks = 0;
const long lesesensor1 = 3000;
const long lesesensor2 = 3000;
//////////////////////////////////////////////////
void setup()
{
pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(start1, INPUT);
pinMode(stop1, INPUT);
pinMode(relais1,OUTPUT);
pinMode(relais2,OUTPUT);
}
//////////////////////////////////////////////////
void loop(){
foerderband();
}
///////////////////////////////////////////////////
void start(){
}
///////////////////////////////////////////////////
void stop(){
}
/////////////////////////////////////////////////
void rechtslauf(){
sensor1State = digitalRead(sensor1);
if (sensor1State == HIGH){
digitalWrite(relais1, HIGH);
digitalWrite(relais2, LOW);
}
}
//////////////////////////////////////////////////
void linkslauf(){
sensor2State = digitalRead(sensor2);
if (sensor2State == HIGH) {
digitalWrite(relais1, LOW);
digitalWrite(relais2, HIGH);
}
}
//////////////////////////////////////////////////
void stopmotor(){
stop1State = digitalRead(stop1);
if (stop1State == HIGH) {
digitalWrite(relais1, LOW);
digitalWrite(relais2, LOW);
}
}
//////////////////////////////////////////////////
void wartezeitrechtslauf()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillisrecht >= lesesensor1)
{
previousMillisrecht = currentMillis;
digitalWrite(relais1, LOW);
digitalWrite(relais2, LOW);
}
}
///////////////////////////////////////////
void wartezeitlinkslauf()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillislinks >= lesesensor2)
{
previousMillislinks = currentMillis;
digitalWrite(relais1, LOW);
digitalWrite(relais2, LOW);
}
}
///////////////////////////////////////////
void foerderband(){
wartezeitrechtslauf();
rechtslauf();
stopmotor();
wartezeitlinkslauf();
linkslauf();
stopmotor();
}