Need help writing a code for turning on and off a solenoid valve.
I also want to add authentication token to use it on Bylnk. an application that is a two way handshake with my smart phone. I know I can do this part I just do not know how to code it.
I would like it to be a switch on Bylnk and when I switch the valve it has a variable?to stay on for 1 hour.
**this is my code I tried to flash to my photon, but it doesnt work. **
class Solenoid{
public:
int led1,led2;
bool toggle = false;
Solenoid(){
Particle.function("solenoidOn",&Solenoid::solenoidOn, this);
led1 = D0;
led2 = D7;
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
int solenoidOn(String extra){
toggle = !toggle;
if(toggle){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
return 1;
}
int onForFour(){
// To blink the LED, first we'll turn it on...
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
// We'll leave it on for 10 second...
delay(4000);
// Then we'll turn it off...
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
//Particle.publish("temp", "hi", PRIVATE);
return 1;
// And repeat!
}
};
/*************************************************************
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
No coding required for direct digital/analog pin operations!
*************************************************************/
#define BLYNK_PRINT Serial // Set serial output for debug prints
//#define BLYNK_DEBUG // Uncomment this to see detailed prints
#include <blynk.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MYtokennotyourtoken";
"but it doesn't work" is not very helpful in terms of understanding what you expect to happen and what you actually see with this code.
Even if you are a beginner with coding, you should at least verbally try to explain your actual issue a bit better.
Thank you for Replying to my post ScruffR
This is my code and authentication token. my solenoid valve is connected to my power source and I have this Relay http://www.microcenter.com/product/476360/5V_Relay_Module so that I can stop the Current and turn the relay on and off when I want the solenoid turned on or off. I used the LED blink code because I figured it only matters that Im turning off and on a digital pin and it doesnt matter what the variable is?
I just want to feed my garden with my phone so I do not have to drive 20 mins to turn a ball valve anymore.
I think perhaps you should revisit the blynk examples.
A particle app would start with some libraries/includes, global variables, friendly pin names etc defines ,
setup() to configure and do one time things and loop() that runs your main program (or just calls blynk if that’s all you’re doing)
The first thing I noticed with that project is that you only got the #include <blynk.h> statement in there but haven’t actually imported the library into your project.
Do that and see again (there is one obvious syntax error)
Thank you all for the help, I have solved this issue. I hope not to offend anyone by saying this but I have been experimenting the arduino. The reference page one their website has been a big help with learning syntax. Though I am back to my Photon and have a working solenoid.
Nice to hear, that you solved your problem. Want to share your code here?
In my opinion you have a 12V solenoid valve, a 5V relay = 2 different power circuits, blynk and your photon.
You can also share your blynk GUI if you like.
No.
You need to incorporate the code of the two respective versions into one of each.
You could rename the two versions to something else and call them sequentially from some "new" setup()/loop() but that's just a non-tech cop-out which may or may not work - especially for loop()