Im trying to make this code not repeat an action if it just done the action. Its lowering a carriage and I dont want it to go down again if its already been lowered if someone accidentally its the lower button again. The same on raising it.
int state;
int last_state = 9;
const int stepPin = 5;
const int dirPin = 2;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
if (Serial.peek() == 'c') {
Serial.read();
state = Serial.parseInt();
if (state == 2) {
if (last_state != 2) {
raise_();
delay(1000);
last_state = state;
}
else {
Serial.println("Already in raised state");
}
}
}
}
}
void raise_() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 3810; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
You have a very complicated nest of IF statements. What is the point of the Serial.peek() - don't you need to remove the character from the buffer even if it is not a 'c'.
Yes there is a serial begin in the code. I just took a sample of the code and posted it. The peek is in there because thats how i was shown to do it. Its a Tkinter gui that sends c0 , c1, etc depending on which button you press.
What happens if anything goes wrong and you get anything other than a 'c' ?
If a byte is available then read it and keep reading available bytes until you get a 'c' then deal with the subsequent character(s) Will the data following the 'c' only ever be 1 character ? If so then you don't need to use parseInt()
Well Ill have to cross that bridge when i get there. In the mean time I ned to figure out how to make a software safety to keep the carraige from driving down or up twice in a row. I guess I could do it in the python script but would be nice just to figure out why the if else using the state/last_state comparative doesnt work.
I got it. It actually does work correctly. It was hanging up in one of the fill function waiting on the probe pin to gnd out before continuing. After manually grounding pin to simulate liquid detection works fine. Thanks for the input and sorry to waste your time.
int state;
int last_state = 9;
int val;
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
const int co2pin = 6;
const int liquid = 7;
const int level_probe = 9;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
pinMode(co2pin, OUTPUT);
pinMode(liquid,OUTPUT);
pinMode(level_probe,INPUT_PULLUP);
digitalWrite(enPin,LOW);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
if (Serial.peek() == 'c') {
Serial.read();
state = Serial.parseInt();
if (state == 2){
if (last_state != 2){
raise_();
delay(1000);
last_state = state;
}
else{
Serial.println("Already in raised state");
}
}
if (state == 1){
if (last_state != 1){
fill();
delay(1000);
last_state = state;
}
else{
Serial.println("Already in fill state");
}
}
if (state == 0){
if (last_state != 0){
lower();
delay(1000);
last_state = state;
}
else{
Serial.println("Already in lowered state");
}
}
}
while (Serial.available() > 0) {
Serial.read();
}
}
}
void raise_() {
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 3810; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
void lower() {
digitalWrite(dirPin,LOW);
for(int x = 0; x < 3810; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
void fill(){
digitalWrite(co2pin,HIGH);
delay(5000);
digitalWrite(co2pin,LOW);
//val = digitalRead(level_probe);
//Serial.println(val);
while(digitalRead(level_probe)==HIGH ) {
digitalWrite(liquid,HIGH);
}
digitalWrite(liquid,LOW);
}
Your state describes the last operation but does not remember that state of the carriage.
If you raised the carriage the state is remembered. However, if you then fill, the state of the carriage is forgotten. What if the next command is to raise the carriage?