PCBAlex
Newbie level 3
I am creating a project that reads a photo interrupter and controls an LED and Servo. The setup mode where the led turns on when the photointerupter in interrupted works fine but the mode where the servo goes off isn't functioning like on the breadboard. I didn't realize that on the breadboard I had the zener diode wired backwards (I corrected this on the PCB). Unfortunately on the PCB it looks like the relay triggers but then the ATTiny85 appears to reset and the process repeats itself rather than going into a completed state. The servo also doesn't activate. Removing the diode doesn't change the result and wiring it backwards just causes the led to slowly dim without the relay going off. I have tried using a schottky diode but that didn't help. See wire diagram and code below. I would be happy to provide any additional information. Thanks for any help in advance.
View attachment Schematic.pdf
View attachment Schematic.pdf
Code:
#include <SoftwareServo.h>
SoftwareServo my_servo;
int ledpin = 0;
int relaypin = 3;
int modepin = 4;
int servopin = 1;
int optopin = A1;
int servh = 8; // Servo home position
int offpos = 92; // Servo position to hit power switch on printer
int ro = 0; // counter to ensure turn off procedure runs once per print
void setup() {
//Serial.begin(9600);
my_servo.attach (servopin);
pinMode(ledpin, OUTPUT);
pinMode(relaypin, OUTPUT);
pinMode(modepin, INPUT);
digitalWrite(ledpin,LOW);
digitalWrite(relaypin,LOW);
delay(50);
}
void loop() {
int modeval = digitalRead(modepin); //checks to see if hardware is in setup mode or standard operating mode
//Serial.println(modeval);
while (ro == 0 && modeval == 1){ //loop ipd untill printer is detected to have finished. runs turn off protocol twice to ensure it worked
ipd();
modeval = digitalRead(modepin);
}
while (modeval == 1){ //runs on completion of ipd to show complete status
fin();
modeval = digitalRead(modepin);
}
sud(); //runs setup mode
ro = 0;
}
void ipd(){
delay(500);
int sensorValue = analogRead(optopin); // Reads the values produced by the photo interruptor on analog pin 0
if ( sensorValue < 2 ){ // If photo interrupror beam is broken
digitalWrite(ledpin, HIGH);
delay(3000); // Gives printer time to cool off hotend
for(int i = 0; i < 2; i++){
my_servo.write (servh); // this will mostly eliminate servo twitch
digitalWrite(relaypin,HIGH);
delay(45);
SoftwareServo::refresh();
smove(offpos); // Turn servo to x degrees to shut of 3D printer buy hitting power switch
smove(servh); // Turn servo to x degrees to home position
}
digitalWrite(ledpin, LOW);
digitalWrite(relaypin,LOW);
ro = 1;
}
}
void fin(){
digitalWrite(ledpin, HIGH);
delay(250);
digitalWrite(ledpin, LOW);
delay(250);
}
void sud() {
int sensorValue = analogRead(optopin); // Reads the values produced by the photo interruptor on analog pin X
//Serial.println(sensorValue);
if ( sensorValue < 2){ // If photo interrupror beam is broken
digitalWrite(ledpin, HIGH);
}
else{
digitalWrite(ledpin, LOW);
}
}
void smove(int x){
for(int i = 0; i < 40; i++){
my_servo.write (x); // Turn servo to x degrees to shut of 3D printer but hitting power switch
delay(25);
SoftwareServo::refresh();
}
}