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
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
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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started