It loads fine but latch does not hold bit during falling or rising cycle of pot
Idea is that stays HIGH at rising cycle and LOW on falling cycle between 400 and 600 thresholds
/* SR Latch NOR gate
*
*
*/
//Global variables
int pot = 5; //potenciometer pin
int val = 0; // 0-1023 value of pot
boolean a1; //reset
boolean b1;
boolean a2; //set
boolean b2;
boolean y1;
boolean y2; //output
void setup(){
Serial.begin(9600);
}
void loop() {
val = analogRead(pot);
y1 != b2;
y2 != b1;
if (a1 || b1){
y1 = true ;
}
if (a2 || b2){
y2 = true ;
}
if (val < 400){ // first threshold
a1 = false;
a2 = true; // set bit
Serial.print("HIGH");
}
if (val > 600){ //second threshold
a1 = true;
a2 = false; // reset bit
Serial.print("LOW ");
}
if ((val <600) && (val>400)){ // hold bit
a1 = false;
a2 = false;
}
if ((y2 == true)&&((val<600)&&(val>400))) {
Serial.print("HIGH2");}
else if ((y2 == false)&&((val<600)&&(val>400))){
Serial.print("LOW 2");}
if (a1 && a2){
Serial.print(" Forbidden ");
}
Serial.print(" ");
Serial.println(val);
}
Does nothing. those are expressions, not assignments.
What are you trying to do? Implement a flip-flop circuit in code?
You do realise flip-flops use positive feedback and cannot be emulated in straight
line code, you have to iterate until stable (not all states are stable)
y1 = !b2;
y2 = !b1;
his code never assigns anything to the
values b2 and b1 so the code doesn't
make sense.
It is always a good idea for a feed back type
setup to have an initial value.
Dwight
y1 = !b2;
y2 = !b1;
his code never assigns anything to the
values b2 and b1 so the code doesn't
make sense.
It is always a good idea for a feed back type
setup to have an initial value.
Dwight
wouldn't the code assign 0 which would be read as false to b1 and b2 thus y1 and y2 would equal true?
Ralja33:
It loads fine but latch does not hold bit during falling or rising cycle of pot
Idea is that stays HIGH at rising cycle and LOW on falling cycle between 400 and 600 thresholds
I spent 10 minutes following this code and it makes no sense especially as what you are asking for sounds simple. Once a output or flag is set it will stay that way until you tell it to do something else.
if you want it to go high when the pot is lower than 400 and stay that way until the pot is higher than 600 that's a simple flag
int pot = A5; //potenciometer pin
int val = 0; // 0-1023 value of pot
byte state=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(pot);
if (val < 400) {
state = 0;
}
if (val > 600) {
state = 1;
}
if (state == 0) {
Serial.println("high");
}
else
{
Serial.println("low");
}
}
if you want to know if the pot is moving and in which direction then you need to record where the pot was and where the pot is. Then compare to see which way its being moved.
else if ((y2 == false)&&((val<600)&&(val>400))){
Serial.print("LOW 2");}
Serial.print(" ");
Serial.println(val);
}
I still don't understand what b1 and b2 do. If there is no value assigned
to them it doesn't make much sense to do anything with
them. If he is assuming they start false, why not just say so.
My guess is that he'd originally tried to create a cross couple
nor latch but got lost somewhere along the way.
Dwight