communication

For a school project i need to make lockers which you can open/close through a 4-digit combination on an arduino. i choose to put a touchscreen on my mega and there for i dont have much of my pins left.
instead of the mega i will use a uno to drive 4 to 6 servo-motor's. i was wondering if i was able to use the TX/RX pins on the mega/uno to send a variable from the mega (master) to the uno (slave) and on that way "open" or "closing" the servo motors. since i have no experience with the TX/RX pins ive tried to do this with digital high/low but this is kind of buggy and not really safe to use in this situation.

as i said, i have no experience with RX/TX communication so what is the best approach i can do with this ? maybe someone has a link to a good turtorial of some example code for me on this problem ?

thnx

Have you looked at the serial class documentation on this site?

there for i dont have much of my pins left.

Have you really used the majority of the 70 IO pins available on the Mega ?

Did you know that you can use the analogue pins as digital IO pins ?

PlatoBE:
For a school project i need to make lockers which you can open/close through a 4-digit combination on an arduino. i choose to put a touchscreen on my mega and there for i dont have much of my pins left.

There are a lot of pins in a Mega. How come you only have a few left?

i was wondering if i was able to use the TX/RX pins on the mega/uno to send a variable from the mega (master) to the uno (slave) and on that way "open" or "closing" the servo motors.

That should be very straightforward. If you are inexperienced it will probably be better to use SofwareSerial on the Uno for communication with the Mega so as to leave Serial available for debugging the Uno program. And use one of Serial1, Serial2 or Serial3 on the Mega for the same reason.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

i dit read this link
http://forum.arduino.cc/index.php?topic=396450
but i dont know if the solution to my problem is in there, i could not really find it

for the pins, i did almost use them all, the only digital IO pins i have left are 8-13. but all my analog pins are not used. as for my rx/tx pins, theyre not used either.

as for the code that i'm using right now this is the pure basics for the mega:

void setup() {
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(8,HIGH); //S1 open
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
Serial.println("S1 open");
delay(500);

digitalWrite(8,LOW); //S2 Open
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
Serial.println("S2 open");
delay(500);

digitalWrite(8,HIGH); // S3 Open
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
Serial.println("S3 open");
delay(500);

digitalWrite(8,HIGH);  //S4 Open
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
Serial.println("S4 open");
delay(500);

digitalWrite(8,LOW); //S1 close
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
Serial.println("S1 close");
delay(500);

digitalWrite(8,LOW); //S2 close
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
Serial.println("S2 close");
delay(500);

digitalWrite(8,HIGH); // S3 close
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
Serial.println("S3 close");
delay(500);

digitalWrite(8,LOW);  // S4 close
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
Serial.println("S4 close");
delay(500);

}

it is just to send a High or a low to a pin on the UNO, then the UNO tells which motor has to to wath with the following code

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int pin2waarde;
int pin4waarde;
int pin7waarde;
int pin8waarde;

int pin2 = 2;
int pin4 = 4;
int pin7 = 7;
int pin8 = 8;

void setup() {
 servo1.attach(6);
 servo2.attach(9);
 servo3.attach(10);
 servo4.attach(11); 

pinMode(pin2,INPUT);
pinMode(pin4,INPUT);
pinMode(pin7,INPUT);
pinMode(pin8,INPUT);

 servo1.write(0);
 servo2.write(0);
 servo3.write(0);
 servo4.write(0);

  Serial.begin(9600);
}

void loop() {
pin2waarde=digitalRead(pin2);
pin4waarde=digitalRead(pin4);
pin7waarde=digitalRead(pin7);
pin8waarde=digitalRead(pin8);

if(pin2waarde != LOW|| pin4waarde != LOW ||pin7waarde != LOW ||pin8waarde != LOW ){
  
if(pin2waarde==HIGH && pin7waarde ==LOW && pin4waarde==LOW && pin8waarde==LOW){
  servo1.write(0);
}

if(pin4waarde==HIGH && pin2waarde == LOW && pin7waarde==LOW && pin8waarde ==LOW){
  servo1.write(180);
}


if(pin7waarde==HIGH && pin2waarde==LOW && pin4waarde==LOW && pin8waarde == LOW){
  servo2.write(0);
}

if(pin8waarde==HIGH && pin2waarde==LOW && pin7waarde==LOW && pin4waarde==LOW){
  servo2.write(180);
}


if(pin2waarde==HIGH && pin4waarde==HIGH && pin7waarde==LOW && pin8waarde == LOW){
  servo3.write(0);
}

if(pin2waarde==HIGH && pin7waarde ==HIGH && pin8waarde == LOW && pin4waarde==LOW){
  servo3.write(180);
}

if(pin2waarde==HIGH&& pin8waarde ==HIGH && pin7waarde==LOW && pin4waarde==LOW){
  servo4.write(0);
}

if(pin4waarde==HIGH && pin7waarde == HIGH && pin8waarde == LOW && pin2waarde==LOW){
  servo4.write(180);
}
}
delay(250);

}

it is probably quite long for what it should do and it might be a project above my programming level/experience but it works this way. the only problem im having are "ghost" signals, sometime it just randomly opens/closes a servo so i thought the TX/RX communication would be a better idea buti have 0 experience with that sort of coding and i have no idea how to aproach it

PlatoBE:
i dit read this link
Serial Input Basics - updated - Introductory Tutorials - Arduino Forum
but i dont know if the solution to my problem is in there, i could not really find it...
...
so i thought the TX/RX communication would be a better idea buti have 0 experience with that sort of coding and i have no idea how to aproach it

What I mean, is the official documentation on this site. This stuff:

he only digital IO pins i have left are 8-13. but all my analog pins are not used

So you have 22 spare pins on the Mega that could be used for digital IO. How many do you need ?

i need about 4 pwm pins for my motors, thats all. i have tried to impliment this in my code already but it did not really work that well. i could not find out why. the motors just started to move very time i touched the touchscreen

so let me just clear up what i'm searching for:

in the code from my mega i have 4 var/int called Lstatus1,Lstatus2,Lstatus3,Lstatus4
They indicate if a lockes is in use, or not. this could be a 1 or a 0.

in my mega/master code i want something where he sends the name of this int and the value of it in the serial communication to my uno/slave

on the unoslave i would like to get something liek this:

while(Serial.available()==0) {}
if (Lstatus==1){
myservo.write(180);
}

if(Lstatus==0){
myservo.write(0);
}

so my question is, how can i sand this information from my mega to my uno and how can i let my uno know which int/value it is

http://forum.arduino.cc/index.php?topic=396450
Serial input basics...

PlatoBE:
i need about 4 pwm pins for my motors, thats all.

Post a list of what you are using each of the pins on your Mega for.

...R

i have tried to impliment this in my code already

Why not post what you tried