Solenoid Valve 12 V

Super Beginner here!

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.

any help would be much appreciated

Peace and love,

Kent D. Rueckert

**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.

1 Like

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.

Thanks for your last post. It looks like you are missing a big chunk of code. Did you application get truncated?

1 Like

The easy way to share your code, Kent is the following:

  • in the WebIDE build.particle.io you will find on the left the button “share this revision”
  • click “get shareable link” and put it here in this thread via
  • a Hyperlink-Funktion (the fourth symbol) in this thread-editor.
    After the community has the chance to give your full code a try.

You can focus to upload a picture from your circuit and error messages you get while you flash the code.

https://go.particle.io/shared_apps/5a2728e605d2ecb80e001e82

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) :wink:

1 Like

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.

1 Like

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.

question can I add two void setups and void loops in one program.

When I compile the code it comes up with this error.

I have a voltage reducer to keep it on one circuit

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()

1 Like

@KyleG @ScruffR
I am now can I set a blynk app and an alexa skill on the same digital pin? they would do the same thing.

particle sells a relay shields. that you may be interested in. I love your cucumber project btw!

Not sure what you're saying exactly, but for what I understand I'd say yes you can.

1 Like

That Typo was quite embarrassing, I should proof read better. I will update you if I get it working!

1 Like

GOT IT :wink:

1 Like