Add Serial.print to get information

Hello dear friends.

I created a code to make a small relay work 10 times and then turn off.
I need to insert a simple magnetic sensor to check if the relay really worked 10 times!

I have no idea how to put the code to read sensor data (hi and low) end read in Serial Monitor!

Any ideas? Below is the code:

int relay = 6;
const int button = 5;
int buttonState = 1;

void setup(){
pinMode(button, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relayPin, HIGH);
}

void loop(){
{
buttonState = digitalRead(button);

if (buttonState == HIGH)

{
for (int counter=1; counter<11; counter = counter+1){
{
digitalWrite(relay, LOW);
delay(2000);
digitalWrite(relay, HIGH);
delay(2000);

Any ideas will be appreciated!

Best regards.

I have no idea how to put the code to read sensor data

digitalRead()

end read in Serial Monitor!

Serial.print()

digitalWrite(relayPin, HIGH);

I don't believe "relayPin" is a valid variable

seems to me you could wire a second relay in series with the relay. the second relay would power an led. just count how many times the led comes on. Or you could wire a relay in parallel with the load on your relay. That relay would send a signal into the controller and you could write code to count how many times the controller receives the signal.

"I need to insert a simple magnetic sensor to check if the relay really worked 10 times!"
What sensor?

Maybe use an opto isolator.
.

If you just simply want to see if the relay worked 10 times, put 5V to the C pin, and a led with a 220ohm resistor on the NO pin and connect to ground. With a 2sec delay, it shouldn't be difficult to watch it for 20 seconds.

What type of arduino is this?

Delta_G:
What type of arduino is this?

Sorry, my msg was deleted before your reply.

Arduino Uno, error:

’serial’ was not declared inthis scope

Below is the code:

#include <SoftwareSerial.h>
int ledPin = 7;
int relayPin = 8;
const int button = 6;
int buttonState = 1;
int analogValue;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(button, INPUT);
  pinMode(relayPin,OUTPUT);
  digitalWrite(ledPin,HIGH);
  digitalWrite(relayPin,HIGH);
  serial.begin(9600);
}

void loop(){
  {
buttonState = digitalRead(button);
analogValue = analogRead(7);

if (buttonState == HIGH)

{
  for (int counter=1; counter<10; counter = counter+1){
  {
    delay(1500);
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    delay(1500);
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    serial.print(analogValue);
  }
}
}
  }
  }

Where did I went wrong?
Thanks.

serial.begin(9600);
'serial' was not declared inthis scope

C++ is case sensitive. Try Serial instead.

analogValue = analogRead(7);

An UNO doesn't have an analog input 7. What are you trying to do here?

serial.begin(9600);
oops
Serial.begin(9600);

Worked like a charm!
One last request, how can I implement a LED to tell me if the counter reached "x" cycles?
Relay must turn on & off 10 times. At the end of 10th. cycle, I need to check by looking and an extra LED.

Thank you.

You don't need any indicator. The for loop will only iterate 9 times the way you have it written so the relays will only pulse 9 times. If you change the initial value of the counter in that for loop to 0 then it will pulse 10 times. You could just put a line right after the for loop to light up the led since at that point in the code you will know that it has fired the relay 10 times.