0

As said no programming skills only started 2 days ago when Nano arrived. Heads in bits. This is how far i got. Any help much appreciated..enter image description here

int EMagnetPin = 8;
int solenoidPin = 9;
int ReedPin = 13;

void setup() 
{
  pinMode(EMagnetPin, OUTPUT);     // assign pin type
  pinMode(solenoidPin, OUTPUT);   // assign pin type
  pinMode(ReedPin, INPUT);        // assign pin type
  Serial.begin(9600);             //serial port open

void loop() 
{
Serial.print(digitalRead(ReedPin));
if (digital REad(ReeddPin)==0 ) {
digitalWrite(solenoidPin,HIGH);
                             
                  // Once magnet applied to Reed Switch Will activate the below

  digitalWrite(EMagnetPin, low);      
  delay(11000);                          
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(500);
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(22500);
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(500);                          // Stops EMagnet
  digitalWrite(solenoidPin, HIGH);     //Start Solenoid   
  delay(500); 
  digitalWrite(solenoidPin, low);      
  delay(500); 
                    // Program Ends 
5
  • 1
    1) Format your code properly by selecting all the code and press the {} icon on the editor menu. 2) What is exactly your question? – hcheung Oct 30 '20 at 1:30
  • What error are you getting what is the exact thing you need can you explain your project idea also – Maaz Sk Oct 30 '20 at 2:58
  • 1
    I formatted your code (select it, Ctrl+K) and would have amended your subject line to be more meaningful but I have no idea what the problem is. What do you want it to do? What does it do? – Nick Gammon Oct 30 '20 at 4:16
  • Use uint8_t instead of int for the variables that will never have a value higher than 255. You'll save memory space by using the smallest variable type possible. – stevieb Oct 30 '20 at 18:04
  • 1
    You didn't ask a question. What are you trying to do, and how is your project failing to accomplish that goal? – Duncan C Oct 30 '20 at 21:48
1

I presume you got some errors, like this:

/tmp/arduino_modified_sketch_836883/sketch_oct30a.ino: In function 'void setup()':
sketch_oct30a:13:3: error: a function-definition is not allowed here before '{' token
   {
   ^
sketch_oct30a:37:17: error: expected '}' at end of input
       delay(500);
                 ^
exit status 1
a function-definition is not allowed here before '{' token

You have to read those. At the end of a function you need a closing brace. So, setup for example would look like this:

void setup() 
{
  pinMode(EMagnetPin, OUTPUT);     // assign pin type
  pinMode(solenoidPin, OUTPUT);   // assign pin type
  pinMode(ReedPin, INPUT);        // assign pin type
  Serial.begin(9600);             //serial port open
}   // <------- ADD THIS

Ditto for the end of the loop function.

The next problem is this line:

    if (digital REad(ReeddPin) == 0 ) {

The function "digitalRead" is spelt like that, not as two words. Also you misspelt "ReedPin" as "ReeddPin".

You have to go carefully through your code, look at the error messages, and fix them one by one. C++ is case-sensitive, you can't just thrown in extra spaces in the middle of words, and you have to be consistent with your variable names (ie. not misspell them).


I suggest you do a C++ tutorial, or find an Arduino tutorial. These are basic coding mistakes made by beginners. With a bit of practice, and some working through examples, you will stop making them.

1
#include <Arduino.h>
int EMagnetPin = 8;
int solenoidPin = 9;
int ReedPin = 13;

void setup()
{
  pinMode(EMagnetPin, OUTPUT);  // assign pin type
  pinMode(solenoidPin, OUTPUT); // assign pin type
  pinMode(ReedPin, INPUT);      // assign pin type
  Serial.begin(9600);           //serial port open
}

void loop()
{
  Serial.print(digitalRead(ReedPin));
  if (digitalRead(ReedPin) == 0)//it is known as digitalRead
  {
    digitalWrite(solenoidPin, HIGH);

    // Once magnet applied to Reed Switch Will activate the below

    digitalWrite(EMagnetPin, LOW);
    delay(11000);
    digitalWrite(EMagnetPin, HIGH);
    delay(500);
    digitalWrite(EMagnetPin, LOW);
    delay(500);
    digitalWrite(EMagnetPin, HIGH);
    delay(500);
    digitalWrite(EMagnetPin, LOW);
    delay(22500);
    digitalWrite(EMagnetPin, HIGH);
    delay(500);
    digitalWrite(EMagnetPin, LOW);
    delay(500);                      // Stops EMagnet
    digitalWrite(solenoidPin, HIGH); //Start Solenoid
    delay(500);
    digitalWrite(solenoidPin, LOW);
    delay(500);
  }
}// please close all loops 
// Program Ends

please remember to close all loops ,enter right spelling

I would suggest you to take a tutorial and also use a program like platformio or Arduino on vscode which will help you to write the code as it has auto code completion so you will always speel the right name for variables and also type the right function names.

Hope this helps.

1

Note that others already gave good answers; this is just a way to make your code more elegant.

This part:

  digitalWrite(EMagnetPin, low);      
  delay(11000);                          
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(500);
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(22500);
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 
  digitalWrite(EMagnetPin, low);      
  delay(500);                          // Stops EMagnet
  digitalWrite(solenoidPin, HIGH);     //Start Solenoid   
  delay(500); 
  digitalWrite(solenoidPin, low);      
  delay(500); 

You see there is a lot of duplication (digitalWrite plus delay).

The only things changing are the pin number, the pin state and the delay time (which is mostly 500).

You can create a new function for this, e.g. digitalWriteWithDelay:

void digitalWriteWithDelay(int pinNumber, int pinState, int delayTime = 500)
{
    digitalWrite(pinNumber, pinState);
    delay(delayTime);
}

Now you can rewrite

  digitalWrite(EMagnetPin, low);      
  delay(11000);                          
  digitalWrite(EMagnetPin, HIGH);       
  delay(500); 

to

digitalWriteWithDelay(EMagneticPin, LOW, 11000);
digitalWriteWithDelay(EMagneticPin, HIGH);

and all subsequent lines similarly. Btw, it's best to use LOW and HIGH (keep the capitalization consequent). As you can see in the last line, 500 need not to be passed as it is the default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.