Please check my mysensors code.
-
Hi guys, sorry newbie problem here.
I am building an automated chickenhouse door using FETs and a electric antenna as the linear actuator.
Opening and closing the door is not as simple as switching a pin high or low, it will involve reed switches and two separate output pins to control the motor.
I wish to save the message.getBool() value to a separate bool variable (DOORSTATE).
I have triedDOORSTATE = (message.sensor, message.getBool()?1:0);
within void receive
and various permutations of it but keep getting the compile error
expected unqualified-id before '=' token
Im sure its something simple but havent figured it out as yet...Thanks,
Matt
-
@Matt uppercase is most often used for defines. Is DOORSTATE defined somewhere? Could you post your sketch?
-
@mfalkvidd
Hiya thanks for your reply. Am in bed now so don't have the sketch to hand. In any case its just getting started, nothing in main void yet. Yep I have DOORSTATE defined as boolean in the setup...
I just want to set it to true or false based on what the GW sends....
-
Sorted it out. Should have read
bool DOORSTATE = (message.sensor, message.getBool()?1:0);
or as it turns out, even better would be
bool DOORSTATE = message.getBool();
Didn't realise I had to re-declare the variable within void receive as I had already done it in setup.
Off to work in a minute, but Im hoping void receive passes the value back to void main....
Thanks anyway,
Matt
-
@Matt no it won't. Variables declared within a function (such as setup or receive) will only be available within that function. Otherwise functions would mess with eachother's variables which would be a, well, mess.
If you want the variable to be available in the global scope, it needs to be declared before the first function. See how lastSend is used in the https://www.mysensors.org/build/pulse_power example.
Also, please do not use an uppercase name for variables. Uppercase names are normally only used for constants. It will work, but there is a risk other programmers will be quite confused.
-
@mfalkvidd
Cheers, thanks for the tips. Learn something every day.
-
@mfalkvidd have finished my code, it all compiles but I haven't had a chance to test it yet, am still waiting for a step down converter to arrive as it will all be powered by a 12v car battery/solar charge controller/panel which can go up to 14.4V. The actuator is an electric antenna from the wreckers yard.
There are two reed switches on the door to detect open or closed position. I have added some code to try to unjam the door if it takes longer than 5 seconds to open or close. There is a manual button (debounced) to open or close the door, as well as reset the jam.
If you have time (HAH! Who does three days out from Christmas) I would well appreciate a critical eye on my code.
In particular I'm not sure if the lines in void receive are correct Im not too worried about errors elsewhere, can likely work them out myself, but the mysensors bits I am not too sure about.DoorState = message.getBool(); saveState(1, message.getBool());
as well as the lines that flag to the gateway/controller the door is jammed (last bit of void loop in the else if bit.
send(msg.set(HIGH));
Entire sketch below. Apologies if its hard to read, I am newish to C and prior to that only wrote in basic 30+ years ago....
Many many thanks,
Matt/** * The MySensors Arduino library handles the wireless radio link and protocol * between your home built sensors/actuators and HA controller of choice. * The sensors forms a self healing radio network with optional repeaters. Each * repeater and gateway builds a routing tables in EEPROM which keeps track of the * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad <henrik.ekblad@mysensors.org> * Copyright (C) 2013-2015 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org * Support Forum: http://forum.mysensors.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * ******************************* * * REVISION HISTORY * Version 1.0 - Henrik Ekblad * * DESCRIPTION * Example sketch showing how to control physical relays. * This example will remember relay state after power failure. * http://www.mysensors.org/build/relay */ // Enable debug prints to serial monitor #define MY_DEBUG #define MY_RADIO_NRF24 #include <MySensors.h> #include <Bounce2.h> #define MOTORSW 3 // Motor activation pin #define MOTORPWR 4 // Motor power lead pin #define REED_OPEN 5 // Reed switch pin, pulled low when door is open #define REED_CLOSED 6 // Reed switch pin, pulled low when door is closed #define MANUALSW 7 // Momentary switch to control door bool DoorState; // 0 closed, 1 open bool PrevState; // Used to check for stated change byte jammed=0; unsigned long timer; // Counter to check door closes in time ie isn't jammed. Bounce debouncer = Bounce(); MyMessage msg(2,V_TRIPPED); void setup() { pinMode(MOTORSW, OUTPUT); pinMode(MOTORPWR, OUTPUT); pinMode(REED_OPEN, INPUT_PULLUP); // Using internal pullup. When read switch is closed, will pull this pin to ground. pinMode(REED_CLOSED, INPUT_PULLUP); // Ditto pinMode(MANUALSW, INPUT_PULLUP); // Turn on pullup for switch, will be pulled low when pushed DoorState = loadState(1); PrevState = DoorState; debouncer.attach(MANUALSW); debouncer.interval(50); } void presentation() { sendSketchInfo("ChickenDoor", "1.0"); // Send the sketch version information to the gateway and Controller present(1, S_BINARY); // Register door actuator to Gateway present(2, S_DOOR); // This will go on if door is jammed } void loop() { debouncer.update(); int value = debouncer.read(); if (value == LOW) { DoorState = !DoorState; jammed==0; // Reset the jam send(msg.set(LOW)); } if ((DoorState != PrevState) && (jammed < 2)) { //Something has changed and door isn't jammed switch(DoorState) { case 0: // Close Door timer = millis(); while( digitalRead(REED_CLOSED)==HIGH) { //Drive actuator until door closes then stop digitalWrite(MOTORPWR, HIGH); //Turn on the actuator wait(50); // Actuator wakes up digitalWrite(MOTORSW, HIGH); //Close the door if (millis() - timer > 5000) { //door is jammed jammed++; jiggle(); //open and close again break; } } digitalWrite(MOTORSW, LOW); //Door is closed, turn off actuator switch digitalWrite(MOTORPWR, LOW); //Door is closed, turn off actuator PrevState = DoorState; jammed=0; break; case 1: // Open Door timer = millis(); while( digitalRead(REED_OPEN)==HIGH) { //Drive actuator until door opens then stop digitalWrite(MOTORPWR, HIGH); //Turn on the actuator wait(50); // Actuator wakes up digitalWrite(MOTORSW, HIGH); //Wake up the actuator wait(100); digitalWrite(MOTORSW, LOW); //Close the door if (millis() - timer > 5000) { //door is jammed jammed++; jiggle(); break; } } digitalWrite(MOTORSW, LOW); //Door is closed, turn off actuator switch digitalWrite(MOTORPWR, LOW); //Door is closed, turn off actuator PrevState = DoorState; jammed=0; break; } } else if (jammed >= 2) { send(msg.set(HIGH)); } } void receive(const MyMessage &message) { if (message.type==V_STATUS) { // We only expect one type of message from controller. But we better check anyway. DoorState = message.getBool(); saveState(1, message.getBool()); // Store state in eeprom Serial.print("Incoming change for sensor:"); // Write some debug info Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } } void jiggle() //J j j jiggle it a bit (4 times) { for(int x = 0; x < 4 ; x++) { digitalWrite(MOTORPWR, HIGH); digitalWrite(MOTORSW, HIGH); wait(500); digitalWrite(MOTORSW, LOW); } digitalWrite(MOTORPWR, LOW); digitalWrite(MOTORSW, LOW); PrevState = !DoorState // Reset PrevState to have another go at opening or closing the door. }
-
@Matt actually I do have time Waiting for my connecting flight.
The code looks good to me. Can't see anything that looks weird.
-
@mfalkvidd
Thanks heaps, enjoy your trip