R
R
Roman Blinov2019-02-19 21:01:02
Arduino
Roman Blinov, 2019-02-19 21:01:02

Merge two sketches together, how to do it?

Hello ! please help, there are two sketches and they need to be combined. the first one makes a call to the phone when the shock sensor is triggered, the second type is central locking, I need the first sketch to turn on in the door closing function.
first /// GSM Shock Sensor
#include SoftwareSerial.h
SoftwareSerial gsm(7, 8);
#define LED 13
#define ShockPin 3 // shock sensor
int ShockVal;
#define TELLNUMBER "ATD+79021039665;" // number to which the sensor will call#define pinBOOT 5
void setup() {
Serial.begin(9600);
delay(1000);
gsm.begin(19200);
pinMode(LED, OUTPUT);
pinMode(ShockPin, INPUT);
delay(1000);
digitalWrite(LED, HIGH);
delay(2000);
while(gsm.find("STARTUP"));
gsm.println("ATE0");
while(1){
gsm.println("AT+COPS?");
if (gsm.find("+COPS: 0")) break;
delay(50);
digitalWrite(LED, HIGH);
delay(500);
}
digitalWrite(LED, LOW);
delay(1500);
digitalWrite(LED, HIGH);
delay(250);
digitalWrite(LED, LOW);
}
void loop() {
Serial.println(ShockVal);
ShockVal = digitalRead(ShockPin);
if(ShockVal == LOW){
digitalWrite(LED,HIGH);
gsm.println(TELLNUMBER);
delay(2500);
}
else
{
digitalWrite(LED, LOW);
}
}
second
#include RemoteReceiver.h
int dver=12;
int bag=4;
void setup() {
RemoteReceiver::init(0, 3, showCode);
pinMode(door,OUTPUT);
pinMode(bag,OUTPUT);
}
void loop() {
}
void showCode(unsigned long receivedCode, unsigned int period)
{
Serial.print("Code: ");
Serial.print(receivedCode);
Serial.print(", period duration: ");
Serial print(period);
Serial.println("us.");
// opening and closing doors
if (receivedCode == 411878) // closed
{
digitalWrite(dver,LOW)
}
if (receivedCode == 411810) // open
{
digitalWrite(dver,HIGH);
}
// trunk
if (receivedCode == 411864)
{
digitalWrite(bag,HIGH);
delay(100000);
digitalWrite(bag,LOW);
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Savely, 2019-02-19
@aaland

So, but from where will the showCode procedure be called?

первый /// GSM Датчик удара 
#include 
SoftwareSerial gsm(7, 8); 

int dver=12; 
int bag=4; 

#define LED 13 
#define ShockPin 3 // датчик удара 
int ShockVal; 
#define TELLNUMBER "ATD+79021039665;" // номер на который будет звонить датчик#define pinBOOT 5 

void setup() { 
Serial.begin(9600); 
delay(1000); 
gsm.begin(19200); 
pinMode(LED, OUTPUT); 

pinMode(ShockPin, INPUT); 

delay(1000); 
digitalWrite(LED, HIGH); 

delay(2000); 
while(gsm.find("STARTUP")); 
gsm.println("ATE0"); 

while(1){ 
gsm.println("AT+COPS?"); 
if (gsm.find("+COPS: 0")) break; 
delay(50); 
digitalWrite(LED, HIGH); 
delay(500); 
} 

digitalWrite(LED, LOW); 
delay(1500); 
digitalWrite(LED, HIGH); 
delay(250); 
digitalWrite(LED, LOW); 


RemoteReceiver::init(0, 3, showCode); 
pinMode (dver,OUTPUT); 
pinMode (bag,OUTPUT);

} 



void showCode(unsigned long receivedCode, unsigned int period) 
{ 

Serial.print("Code: "); 
Serial.print(receivedCode); 
Serial.print(", period duration: "); 
Serial.print(period); 
Serial.println("us."); 
// открытие и зокрытие дверей 

if (receivedCode == 411878) // закрыто 
{ 
digitalWrite(dver,LOW) 
} 

if (receivedCode == 411810) // открыто 
{ 
digitalWrite(dver,HIGH); 

} 

// багажник 
if (receivedCode == 411864) 
{ 
digitalWrite(bag,HIGH); 
delay(100000); 
digitalWrite(bag,LOW); 
} 

}


void loop() { 
Serial.println(ShockVal); 
ShockVal = digitalRead(ShockPin); 
if(ShockVal == LOW){ 
digitalWrite(LED,HIGH); 
gsm.println(TELLNUMBER); 
delay(2500); 
} 
else 
{ 
digitalWrite(LED, LOW); 
} 

}

M
Max889900, 2019-05-27
@Max889900

Hello, please help me combine two sketches
Code to create Ping:
#include
#include
#include
#include
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
byte ip[] = {192,168,0,177}; // ip address for ethernet shield
IPAddress pingAddr(0,0,0,0); // ip address to ping
SOCKET pingSocket = 0;
charbuffer[256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
void setup()
{
// start Ethernet
Ethernet.begin(mac, ip);
Serial.begin(9600);
DNSClient dns; dns.getHostByName("ya.ru",pingAddr); // at this point the function works }
dns.begin(Ethernet.dnsServerIP());
void loop()
{
ICMPEchoReply echoReply = ping(pingAddr, 4);
if (echoReply.status == SUCCESS)
{
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
echoReply. data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
}
else
{
sprintf(buffer, "Echo request failed; %d", echoReply.status);
}
Serial.println(buffer);
delay(500);
}
Code for sending a message:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//structure for the message
struct Message
{
String From;//sender's address
String To;// recipient's address
String Subject;// Message subject
String Body;// Message body
};
//mac address of the device (may not be changed)
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x05 };
// IP address of the adruiny (can not be changed)
IPAddress ip(192,168,1,48);
// IP smtp server
IPAddress server(87,250,251,50);
// IP of other servers can be found on the internet
// login and password for the mail service
char login[] = "[email protected]";//
char password[] = "A.......";
// create an instance of the client
EthernetClient client;
void setup(){
Serial.begin(9600);// UART
Ethernet.begin(mac,ip);// Run the shield
Serial.println("Start");
}
void loop(){
while(Serial.available()){
char inSerialChar = 0;
inSerialChar = Serial.read();
if(inSerialChar == 's'){
SendMessage();// When sending to duin 's', send a message.
}
}
}
// method to connect to the server
bool connectToServer(){
if(client.connected()){
}
else{
if(client.connect(server,25)){
return true;
}
else{
return false;
}
}
}
// message sending method
void SendMessage(){
Serial. println("Please wite…");
logpass();
Message message = {
"[email protected]",
"[email protected]",
"Pen test",//subject
"Hello from Arduino!!!! " };//the message itself
client.println("MAIL From:<"+message.From+">");
delay(250);
client.println("
delay(250);
client.println("DATA");
delay(250);
client.println("From:"+message.From);
client.println("To:"+message.To);
client.println("Subject:"+message.Subject);
delay(250);
client.println(message.Body);
delay(250);
client.println(".");// when a point arrives, the server queues the message
delay(250); // to send
client.println("QUIT");// as soon as the connection is closed, the message flies away
delay(250);
Serial.println("Message sending...");
}
// connection to the server and authorization
void LogPass(){
char array[64];// array for encoding
if(connectToServer()){
client.
delay(250);
client.println(F("AUTH LOGIN"));
delay(250);
client.println(strToBase64(login));
delay(250);
client.println(strToBase64(password));
delay(250);
}
else{
Serial.println("Server not connected…");
}
}
// Base64 encoding
String strToBase64(char str[]){
char charArray[64];
base64_encode(charArray,str,strlen(str));
Stringresult = charArray;
return result;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question