Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help on the if statement where if (pulsee==8 && metall==1023). the soleno

ID: 3842690 • Letter: I

Question

I need help on the if statement where  if (pulsee==8 && metall==1023). the solenoid activates but it keeps going and I just want it to turn on once, like when the pulse is 4 it works fine.

#include <Servo.h>
int solenoidPin = 8; //This is the output pin on the Arduino we
// Constants
const int coinpin = 2;
const int targetcents =20 ;
int servoPin = 9;
Servo servo;
int angle = 0; // servo position in degrees

float metal, metall;
int reading;
int metalPin = A0;
int metalPinn = A1;


// Variables
volatile int pulse = 0;
volatile int pulsee = 0;
int pesos = 0;

// Setup
void setup() {
  
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
// Servo motor///////
servo.attach(servoPin);

// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output

}


// Main loop
void loop() {
reading = analogRead(metalPin);
metal = (float)reading*100/1024.0;
Serial.print("Metal in Proximity = ");
Serial.println(metal);
reading = analogRead(metalPinn);
metall = (float)reading;
Serial.print("Metall in Proximity = ");
Serial.println(metall);

if (pulse >= targetcents) {
pesos = pesos;
pulse = pulse - targetcents;
}

if (pulsee >= targetcents) {
pesos = pesos;
pulsee = pulsee - targetcents;
}

// If we haven't reached o ur target, keep waiting...
else {
}
// Debugging zone
  

Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(10);
  
if (pulse==2) {
pesos=1;

}
Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(10);
if (pulse==4) {
pesos=2;
}
Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(10);


if (pulse==8) {
pesos=4;
}
Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(10);


if (pulse==10) {
pesos=5;
}
Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(10);


if (pulse==20) {
pesos=10;
}
Serial.print(pesos);
Serial.println(" Pesos insertados");
delay(1000);
if (pulse==20) {
pesos=10;
// scan from 0 to 180 degrees
for(angle = 0; angle < 90; angle++)
{
servo.write(angle);   
delay(2);   
}
// now scan back from 180 to 0 degrees
for(angle = 90; angle >5; angle--)
{
servo.write(angle);   
delay(2);   
}


}
Serial.print(pesos);
Serial.println(" pesos insertados");
delay(10);

if (pulsee==4 && metall==1023) {
pesos=50;
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(2000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(2000); //Wait 1 Second
while(digitalRead(pulsee)==HIGH);
  
}
if (pulsee==8 && metall==1023) {
pesos=50;
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(2000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(2000); //Wait 1 Second
while(digitalRead(pulsee)==HIGH);
  
}
  
  
}

// Interrupt
void coinInterrupt(){
  
// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
pulse = pulse + 1;
pulsee=pulsee+1;
  
}

Explanation / Answer

delay(10);

if (pulsee==8 && metall==1023) {
pesos=50;
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Second
while(digitalRead(pulsee)==HIGH);
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote