Прикупил тут arduino Pro mini +Ardumoto+RF433+ пару актуаторов для солнечных батарей.
На сайтах про arduino(arduino.cc и arduino.ru) нашел пару скетчей и попытался объединить для управления воротами с брелка от GSM сигнализации.
пока на светодиодах все работает
может-кому нибудь потребуется.
За комментарии и советы буду благодарен.
Хотелось бы еще добавить
1) управление через Uart для управления с MDM или другой arduino
2) пару соников для обнаружения перпятствий,
3) управление двумя электромеханическими замками(калитка и замок на воротах).
И у кого есть опыт как организовать питание и от сети (БП на 12V и аккумулятора на 12В, в случае отсутствия сети).
Спасибо.
Ну сам скетч.
Сознательно не использовал при управлении через RF mode 1,3
Код: Выделить всё
/*Simple Gate Controller*/
/*
Author: tammat
Email: tamatvey@gmail.com
Date: 21 Jan 2014
Description: This is just a simple PWM driven Electric gate System. A replacement
board for the existing fried board due to lightning strikes. It works the same and
performs better than the original system.
Next phase for this code will be using some electronics current sensor for
detecting the load of the motors so it will know when to cut off the power of the
gate instead of using the delay as the timing to open or close the gate.
*/
#include <RemoteReceiver.h>
#define REMOTE_BUTTON_1 8
#define REMOTE_BUTTON_2 9
#define GATE_A_DIRECTION 12
#define GATE_B_DIRECTION 13
#define PWM_A 3
#define PWM_B 11
#define RUN 1
#define STOP 0
//Define RC Key on Remote Control
#define GATE_1 000000 // Unlock
#define GATE_2 000000 // Lock
#define GATE_3 000000 // S
#define GATE_4 000000 // Alarm
// Variables
int remote_button_1_state = LOW; // Getting the reading from the remote controller
int remote_button_2_state = LOW; // Getting the reading from the remote controller
int gate_1_direction_state = LOW; // LOW = close, HIGH = open
int gate_2_direction_state = HIGH; // LOW = close, HIGH = open
int gate_function = 0; // 0 = do nothing (idle)
// 1 = button one pressed
// 2 = button two pressed
// 3 = button one and two pressed
boolean Code=false;
unsigned long lastTick = 0;
unsigned long lastDebounce = 0;
unsigned long code;
void detect_remote_command() {
unsigned long lastTick = 0;
// Delay for 2 seconds before starting the detection process
delay(500);
lastTick = millis();
Serial.println("Detecting remote command while executing motor process.");
while(millis() - lastTick < 17000) {//17 seconds - time of work
// If detected some commands from the remote....emergency stop
// Getting data from the remote controller
remote_button_1_state = digitalRead(REMOTE_BUTTON_1);
remote_button_2_state = digitalRead(REMOTE_BUTTON_2);
if (code!=00000)
{
code=00000;
motor(0, 0, STOP);
motor(1, 0, STOP);
Serial.println("EMERGENCY STOPPED the MOTORS as remote key signal received.");
delay(500);
return;
}
if(remote_button_1_state == LOW || remote_button_2_state == LOW) {
// Stop all the motors...
motor(0, 0, STOP);
motor(1, 0, STOP);
Serial.println("EMERGENCY STOPPED the MOTORS as remote key signal received.");
delay(500);
return;
}
}
// Stops all motor now
motor(0, 0, STOP);
motor(1, 0, STOP);
Serial.println("Normal stopping on Motors");
}
void motor(int id, int direction, int mode) {
if(id == 0) {
if(mode == RUN) {
if(direction == 0) {
digitalWrite(GATE_A_DIRECTION, LOW);
}
else {
digitalWrite(GATE_A_DIRECTION, HIGH);
}
digitalWrite(PWM_A, HIGH);
}
else {
digitalWrite(GATE_A_DIRECTION, direction);
digitalWrite(PWM_A, LOW);
}
}
if(id == 1) {
if(mode == RUN) {
if(direction == 0) {
digitalWrite(GATE_B_DIRECTION, LOW);
}
else {
digitalWrite(GATE_B_DIRECTION, HIGH);
}
digitalWrite(PWM_B, HIGH);
}
else {
digitalWrite(GATE_B_DIRECTION, direction);
digitalWrite(PWM_B, LOW);
}
}
}
void gate_command(int mode) {
switch(mode) {
case 1:
// Open/Close gate A
gate_1_direction_state = !gate_1_direction_state;
Serial.print("GATE A DIRECTION (BUTTON 1): ");
Serial.println(gate_1_direction_state);
// Initiate open gate process
motor(0, gate_1_direction_state, RUN);
// Detect button press for emergency stop opening or closing gate
detect_remote_command();
break;
case 2:
// Open/Close gate A & B
gate_1_direction_state = !gate_1_direction_state;
gate_2_direction_state = !gate_2_direction_state;
// Serial.println("GATE DIRECTION (BUTTON 2)");
if (gate_1_direction_state==1 &&gate_2_direction_state==0)
{
Serial.println("Opening...");
motor(0, gate_1_direction_state, RUN);
delay(1000);//Задержка одной створки при открытии
motor(1, gate_2_direction_state, RUN);
}
if (gate_1_direction_state==0 && gate_2_direction_state==1)
{
Serial.println("Closing...");
motor(1, gate_2_direction_state, RUN);
delay(1000);//Задержка одной створки при открытии
motor(0, gate_1_direction_state, RUN);
}
/* Serial.print("GATE A DIRECTION: ");
Serial.println(gate_1_direction_state);
Serial.print("GATE B DIRECTION: ");
Serial.println(gate_2_direction_state);
// Initiate open gate process
motor(0, gate_1_direction_state, RUN);
motor(1, gate_2_direction_state, RUN);
*/
// Detect button press for emergency stop opening or closing gate
detect_remote_command();
code =000000;
break;
case 3:
// Open/Close gate B
gate_2_direction_state = !gate_2_direction_state;
Serial.print("GATE B DIRECTION (BUTTON 1 and BUTTON 2): ");
Serial.println(gate_2_direction_state);
// Initiate open gate process
motor(1, gate_2_direction_state, RUN);
// Detect button press for emergency stop opening or closing gate
detect_remote_command();
}
}
void setup() {
Serial.begin(9600);
Serial.println("Starting GateDuino Controller Version 1.0 alpha");
Serial.println("Initializing Parameters");
pinMode(REMOTE_BUTTON_1, INPUT); // set the pin as input
digitalWrite(REMOTE_BUTTON_1, HIGH); // turn on the internal pullup resistor 20k
pinMode(REMOTE_BUTTON_2, INPUT); // set the pin as input
digitalWrite(REMOTE_BUTTON_2, HIGH); // turn on the internal pullup resistor 20k
pinMode(GATE_A_DIRECTION, OUTPUT); // set the pin as output
pinMode(GATE_B_DIRECTION, OUTPUT); // set the pin as output
pinMode(PWM_A, OUTPUT); // set the pin as output
pinMode(PWM_B, OUTPUT); // set the pin as output
RemoteReceiver::init(0, 1, showCode);
}
void loop() {
// Getting data from the remote controller
remote_button_1_state = digitalRead(REMOTE_BUTTON_1);
remote_button_2_state = digitalRead(REMOTE_BUTTON_2);
if (code==GATE_1){
code=0000000;
gate_command(2);
Serial.println("Drive from Remote Control");
}
// Debugging only will be removed
/* Serial.print("BUTTON (1:2): ");
Serial.print(remote_button_1_state);
Serial.print(":");
Serial.println(remote_button_2_state);
*/
// When getting the button 2 then will try to open or close the gate based on the previous gate state.
if(remote_button_1_state == LOW && remote_button_2_state == LOW) {
gate_command(3);
}
if(remote_button_1_state == HIGH && remote_button_2_state == LOW) {
gate_command(2);
}
if(remote_button_1_state == LOW && remote_button_2_state == HIGH) {
gate_command(1);
}
if(remote_button_1_state == HIGH && remote_button_2_state == HIGH) {
// Do nothing....
// gate_command(0);
}
delay(500);
}
void showCode(unsigned long receivedCode, unsigned int period) {
if (code != receivedCode){
code = receivedCode;
Serial.print("Code: ");
Serial.println(receivedCode);
}
/* Serial.print(", period duration: ");
Serial.print(period);
Serial.println("us.");*/
}