how do i make sequence for program ? i have 3 sketches and want to combine it . if sketch 1 is no, it will go to sketch 2, and if it is a no, it will go to sketch 3. what i have to do ?
Call your "loop" functions "loop0", loop1", "loop2" etc.
how can i call it sir ? at the void loop ? it cant be done
I gave a vague answer because you asked a vague question.
If you want a more detailed answer, you must provide more detail.
my bad.. sorry sir .. here is my first sketches
sketch 1
<
int Relay1 = 2; // Actuator 1
int Relay2 = 1; // Actuator 1
int Relay7 = 8; // Actuator 2
int Relay8 = 7 ; // Actuator 2
int Sensor1 = 11;
int Sensor2 = 12;
int val;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); // Actuator 1
pinMode (Relay2, OUTPUT); // Actuator 1
pinMode (Relay7, OUTPUT); // Actuator 2
pinMode (Relay8, OUTPUT); // Actuator 2
pinMode (Sensor1, INPUT); // Actuator 1
pinMode (Sensor2, INPUT); // Actuator 2
}
void loop()
{
if (val == digitalRead(Sensor1)) // Actuator 1
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
delay (200);
}
else {
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
delay (200);
}
if (val == digitalRead(Sensor1)) // Actuator 1
{
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
delay (1000);
}
else {
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
delay (2000);
}
if (val == digitalRead(Sensor2)) // Actuator 1
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
delay (200);
}
else {
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
delay (200);
}
if (val == digitalRead(Sensor2)) // Actuator 1
{
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
delay (1000);
}
else {
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
delay (2000);
}
}
Sketch 2
<
int Relay1=2; // Actuator 1
int Relay2=1; // Actuator 1
int Relay5=5; // Actuator 2
int Relay6=6; // Actuator 2
int Sensor3=13; // Thru Beam
//int Sensor2=12;
int val;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); // Actuator 1
pinMode (Relay2, OUTPUT); // Actuator 1
pinMode (Relay5, OUTPUT); // Actuator 2
pinMode (Relay6, OUTPUT); // Actuator 2
pinMode (Sensor3, INPUT); // Actuator 1
//pinMode (Sensor2, INPUT); // Actuator 2
}
void loop()
{
if (val == digitalRead(Sensor3)) // Actuator 1
{
digitalWrite (Relay6, HIGH);
digitalWrite (Relay5, LOW);
delay (200);
}
else {
digitalWrite (Relay6, LOW);
digitalWrite (Relay5, HIGH);
delay (200);
}
if (val == digitalRead(Sensor3)) // Actuator 1
{
digitalWrite (Relay2, HIGH);
digitalWrite (Relay1, LOW);
delay (200);
}
else {
digitalWrite (Relay2, LOW);
digitalWrite (Relay1, HIGH);
delay (200);
}
}
sketch 3
<
int PIR=10;
int val;
void setup() {
pinMode (PIR, INPUT);
pinMode (Relay3, OUTPUT);
pinMode (Relay4, OUTPUT);
void loop ()
{
if (val == digitalRead(PIR))
{
digitalWrite (Relay3, HIGH);
digitalWrite (Relay4, LOW);
}
else {
digitalWrite (Relay3, LOW);
digitalWrite (Relay4, HIGH);
}
}
here is what i want to do. if sketch 1 is a no so it will go to sketch 2. if the skectch 2 is a no and it will go to sketch 3. end
if sketch 1 is a no
How can a sketch be a no?
if sketch 1 is a no so it will go to sketch 2.
I don't understand the concept of a sketch that may or may not be a "no".
Please explain.
Please also use code tags when posting code.
it mean that the sensor 1 is not detect anything so it will move to sketch 2
Your code is very confusing, with sensors which appear to also be actuators, and "val" which only ever has its default value of zero.
Other than that, I can't really see why what you want to do shouldn't be achievable.
Maybe you need to refine your specification a little first.
okayy sorry...
here is the renew code
sketch 1
int Relay1 = 2; // Actuator 1
int Relay2 = 1; // Actuator 1
int Relay7 = 8; // Actuator 2
int Relay8 = 7 ; // Actuator 2
int Sensor1 = 11; // inductive sensor 1
int Sensor2 = 12; // inductive sensor 2
int val;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); // Actuator 1
pinMode (Relay2, OUTPUT); // Actuator 1
pinMode (Relay7, OUTPUT); // Actuator 2
pinMode (Relay8, OUTPUT); // Actuator 2
pinMode (Sensor1, INPUT); // inductive sensor 1
pinMode (Sensor2, INPUT); // inductive sensor 2
}
void loop()
{
if (val == digitalRead(Sensor1))
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
delay (200);
}
else {
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
delay (200);
}
if (val == digitalRead(Sensor1))
{
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
delay (1000);
}
else {
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
delay (2000);
}
if (val == digitalRead(Sensor2))
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
delay (200);
}
else {
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
delay (200);
}
if (val == digitalRead(Sensor2))
{
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
delay (2000);
}
else {
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
delay (4000);
}
}
sketch 2
<
int Relay1=2; // Actuator 1
int Relay2=1; // Actuator 1
int Relay5=5; // Actuator 2
int Relay6=6; // Actuator 2
int Sensor3=13; // Thru Beam
int val;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); // Actuator 1
pinMode (Relay2, OUTPUT); // Actuator 1
pinMode (Relay5, OUTPUT); // Actuator 3
pinMode (Relay6, OUTPUT); // Actuator 3
pinMode (Sensor3, INPUT); // thru beam sensor
}
void loop()
{
if (val == digitalRead(Sensor3)) // thru beam sensor
{
digitalWrite (Relay6, HIGH);
digitalWrite (Relay5, LOW);
delay (200);
}
else {
digitalWrite (Relay6, LOW);
digitalWrite (Relay5, HIGH);
delay (200);
}
if (val == digitalRead(Sensor3)) // thru beam sensor
{
digitalWrite (Relay2, HIGH);
digitalWrite (Relay1, LOW);
delay (200);
}
else {
digitalWrite (Relay2, LOW);
digitalWrite (Relay1, HIGH);
delay (200);
}
}
sketch 3
<
int Relay3=3;
int Relay4=4;
int PIR=10;
int val;
void setup() {
pinMode (PIR, INPUT); // PIR sensor
pinMode (Relay3, OUTPUT); // Actuator 4
pinMode (Relay4, OUTPUT); // Actuator 4
void loop ()
{
if (val == digitalRead(PIR)) // PIR sensor
{
digitalWrite (Relay3, HIGH);
digitalWrite (Relay4, LOW);
}
else {
digitalWrite (Relay3, LOW);
digitalWrite (Relay4, HIGH);
}
}
Yeah, we really need you to work on those code tags.
To be honest, that looks pretty similar to the code you posted earlier.
All of these if (val == digitalRead(Sensor3)) may as well simply be written if (LOW == digitalRead(Sensor3)) - it would make the intention a lot clearer
int Relay1 = 2; // Actuator 1
int Relay2 = 1; // Actuator 1
int Relay7 = 8; // Actuator 2
int Relay8 = 7 ; // Actuator 2
See the nice looking code box? Why are you not using them?
The code and comments make no sense. Is it a relay or an actuator? How many relay/actuators are there?
Why does Relay1 equal 2 while Relay2 equals 1?
What, exactly, in sketch 1 would mean that "sketch one is no"?
it actually a relay that control actuator.. there are 4 actuator that i using so the is 8 relay i use .. i just label the relay as an actuator so i know which one control which one. Relay 1=2 because relay input is connected to digital input 2 and vice versa. this code work greatly in seperate sketch.
Sketch 1 would mean no if sensor 1 not detecting the object and would make skectch 2 activated
Sketch 1 would mean no if sensor 1 not detecting the object
Not sensing it with sensor 1? Not sensing it with sensor 2? Not sensing it with either sensor?
You have code for when sketch 1 is not sensing anything with sensor 1. You have code for when sensor 2 is not sensing anything. What should happen to that code?
Can you see why you need to be VERY precise in explaining what you want to do?
look, it is easier if you put your required logic into simple english like this:
if (signal is true on sensor 1)
{
//do something
}
else if (signal is false on sensor 1 AND signal is true on sensor 2)
{
//do something else
}
Try to create some 'pseudo code' to describe your inputs and outputs as I showed you here
if english isn't you first language it can be difficult to be as precise as you need... as @PaulS pointed out.
I have make the coding but it seems it not using the sensors as its input. It just go the flow like timer.
Take a look pls at my coding
int Relay1=2;
int Relay2=1;
int Relay3=3;
int Relay4=4;
int Relay5=5;
int Relay6=6;
int Relay7=7;
int Relay8=8;
int Sensor1=11;
int Sensor2=12;
int Sensor3=13;
int PIR=10;
int val=LOW;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); //
pinMode (Relay2, OUTPUT); //
pinMode (Relay3, OUTPUT); //
pinMode (Relay4, OUTPUT); //
pinMode (Relay5, OUTPUT); //
pinMode (Relay6, OUTPUT); //
pinMode (Relay7, OUTPUT); //
pinMode (Relay8, OUTPUT); //
pinMode (Sensor1,INPUT); // Inductive 1
pinMode (Sensor2,INPUT); // Inductive 2
pinMode (Sensor3,INPUT); // Thru beam
pinMode (PIR, INPUT); // PIR Sensor
}
void loop() {
// put your main code here, to run repeatedly:
Reset();
delay (2000);
do {
if (val == digitalRead(PIR))
delay (2000);
{
if (val == digitalRead(Sensor1) && val == digitalRead(Sensor2))
{
MetalForward();
delay (7000);
MetalReverse();
delay (7000);
}
else if (val == digitalRead(Sensor3))
{
PaperForward();
delay (7000);
PaperReverse();
delay (7000);
}
else
{
BottleForward();
delay(2000);
BottleReverse();
delay (2000);
}
}
} while (1);
}
void MetalForward ()
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
}
void MetalReverse ()
{
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
}
void PaperForward ()
{
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
digitalWrite (Relay5, LOW);
digitalWrite (Relay6, HIGH);
}
void PaperReverse ()
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay5, HIGH);
digitalWrite (Relay6, LOW);
}
void BottleForward ()
{
digitalWrite (Relay3, LOW);
digitalWrite (Relay4, HIGH);
}
void BottleReverse ()
{
digitalWrite (Relay3, HIGH);
digitalWrite (Relay4, LOW);
}
void Reset ()
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay3, HIGH);
digitalWrite (Relay4, LOW);
digitalWrite (Relay6, HIGH);
digitalWrite (Relay5, LOW);
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
}
Why do you have an infinite do-while inside the loop() function, which does the same thing?
im not really sure .. pls correct me if im wrong
Hi,
What is the application?
What are your inputs?
What are your outputs?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
We have no idea how you have your inputs wired or how you have your output relays configured.
A picture of your project will be worth more than the odd thousand words wasted here.
What is your electronics, programming, arduino, hardware experience?
Sorry to ask these questions, but it doesn't look as if this thread has progressed since post #1.
Thanks.. Tom.... ![]()
the application is about sorting the recycled item which is plastic bottle, tin , and paper
my input is 2 inductive sensor for tin,
1 through beam sensor for paper,
1 PIR sensor for motion sensor if either of this two is not sensing anything for plastic bottle .
my output is 4 linear actuator controlled by relay
Im just started using arduino this month.
here is my coding
int Relay1=2; // connected to digital 2
int Relay2=1; // connected to digital 1
int Relay3=3; // connected to digital 3
int Relay4=4; // connected to digital 4
int Relay5=5; // connected to digital 5
int Relay6=6; // connected to digital 6
int Relay7=7; // connected to digital 7
int Relay8=8; // connected to digital 8
int Sensor1=11; // Inductive Sensor 1
int Sensor2=12; // Inductive Sensor 2
int Sensor3=13; // Through Beam
int PIR=10; // PIR sensor
int val=LOW;
void setup() {
// put your setup code here, to run once:
pinMode (Relay1, OUTPUT); // Actuator 1
pinMode (Relay2, OUTPUT); // Actuator 1
pinMode (Relay3, OUTPUT); // Actuator Trapdoor 1
pinMode (Relay4, OUTPUT); // Actuator Trapdoor 1
pinMode (Relay5, OUTPUT); // Actuator 2
pinMode (Relay6, OUTPUT); // Actuator 2
pinMode (Relay7, OUTPUT); // Actuator Trapdoor 2
pinMode (Relay8, OUTPUT); // Aactuator Trapdoor 2
pinMode (Sensor1,INPUT); // Inductive 1
pinMode (Sensor2,INPUT); // Inductive 2
pinMode (Sensor3,INPUT); // Thru beam
pinMode (PIR, INPUT); // PIR Sensor
}
void loop() {
// put your main code here, to run repeatedly:
Reset();
delay (2000);
do {
if (val == digitalRead(PIR))
delay (2000);
{
if (val == digitalRead(Sensor1) && val == digitalRead(Sensor2)) // If inductive sensor sense (Both)
{
MetalForward();
}
else if (val == digitalRead(Sensor3)) // if Through beam sensor sense
{
PaperForward();
}
else // if both sensor not sense
{
Bottle();
}
}
} while (1);
}
void MetalForward () // Actuator 1 && Actuator Trapdoor 2
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay7, HIGH);
digitalWrite (Relay8, LOW);
}
void MetalReverse () // Actuator 1 && Actuator Trapdoor 2
{
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
}
void PaperForward () // Actuator 1 && Actuator 2
{
digitalWrite (Relay1, LOW);
digitalWrite (Relay2, HIGH);
digitalWrite (Relay5, LOW);
digitalWrite (Relay6, HIGH);
}
void PaperReverse () // Actuator 1 && Actuator 2
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay5, HIGH);
digitalWrite (Relay6, LOW);
}
void Bottle () // Actuator Trapdoor 1
{
digitalWrite (Relay3, LOW);
digitalWrite (Relay4, HIGH);
}
void Reset () // Reset all in default position
{
digitalWrite (Relay1, HIGH);
digitalWrite (Relay2, LOW);
digitalWrite (Relay3, HIGH);
digitalWrite (Relay4, LOW);
digitalWrite (Relay6, HIGH);
digitalWrite (Relay5, LOW);
digitalWrite (Relay7, LOW);
digitalWrite (Relay8, HIGH);
}