Boolean simply won't work, am i missing something here? And millis aswell?

Hello again!

I have a weird way of coding, i know. i’m lazy and had to do this quick because of a weekly seminar im following.
Making an electric wire game, but, it comes down to this.

Pulse isn’t lowering each millis cycle. How come?
And aswell the boolean thing, i’m trying to switch sides that require input, that drive leds, and increase the pulse integer.
I’m really overlooking something here, but i’m getting a bit crazy looking at the same thing over and over again.

All help will be greatly apreciated!

regards,
Alex

Code:

 int TIP120pin = 11; //for this project, I pick Arduino's PMW pin 11
int potmeter = A0;
int reading = 0;
int readingmapped = 0;
int ledpin1 = 6;
int ledpin2 = 7;
boolean switcheroo;
int stroomdraad = A1;
int stroomjuist1 = A2;
int stroomjuist2 = A3;
int pulse = 255;
unsigned long previousMillis = 0; 
const long interval =1000;
void setup()

{
  switcheroo = false;
pinMode(stroomdraad, INPUT);
pinMode(stroomjuist1 ,INPUT);
pinMode(stroomjuist2, INPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(potmeter, INPUT);
pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin
analogWrite(TIP120pin, 125); // By changing values from 0 to 255 you can control motor speed
Serial.begin(9600); 

}


void loop()
{
    unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
  
    pulse - 20;
  }
Serial.println("Pulse ");
Serial.print(pulse);

if (analogRead(stroomdraad) == 1023)
{pulse = pulse - 10;

//delay(10);
}
if (switcheroo == false){
  digitalWrite(ledpin1, HIGH);
  digitalWrite(ledpin2,LOW);
  
}
if (switcheroo == true)
{
digitalWrite(ledpin1,LOW);
digitalWrite(ledpin2,HIGH);
}
if (analogRead(stroomjuist1) == 1023 && switcheroo == false)
{
  pulse= pulse + 200;
  switcheroo == true;
}
if (analogRead(stroomjuist2) == 1023 && switcheroo == true)
{ pulse = pulse + 200;
switcheroo == false;
}

  if (pulse < 25)
{pulse = 25;}
  if (pulse > 255)
  {pulse = 255;}
//reading =  analogRead(potmeter);
 // readingmapped = map(reading,0,1024,0,255);
  
   analogWrite(TIP120pin,pulse);
  //analogWrite(TIP120pin, readingmapped);
delay(100);
 // Serial.println(analogRead(stroomdraad));
  
}

Try changing this line: pulse - 20; pulse = pulse - 20;

CrossRoads: Try changing this line: pulse - 20; pulse = pulse - 20;

Thanks sir Roads, this was the culprit of the pulse integer not lowering?

I will be only able to test it out tomorrow morning, which is a few hours before this thing gotta work.

I've been put on technical duty by my team and its kinda allot by myself. This is one of the few interactive objects.. thank atmega that the rest of stuff kinda works though.

And for the ledpin1, and ledpin2 not switching im still confused why it won't "switch". Thought it would work in the way its coded now. The if statements.

if (switcheroo == false){
  digitalWrite(ledpin1, HIGH);
  digitalWrite(ledpin2,LOW);
  
}
if (switcheroo == true)
{
digitalWrite(ledpin1,LOW);
digitalWrite(ledpin2,HIGH);
}

Well, stylistically there's a few problems* but logically it should work. What are you seeing? LED1 stays on or LED2 stays on? If neither is on then there's a problem with your wiring.

  • (1) You could just use an else instead of testing the variable again for the opposite condition. Booleans can't be anything else except true or false. (2) You don't normally test for if(value==true). Just write if(value). Then rearrange the true and false sections to put the false one in the else block.

Can't seem to be able quote through the iphone. Website keeps hanging with a "loading" banner.

@MorganS i really should work on my coding grammar. But are you sure it isn't logical? So wiring is the problem? Led1 stays on? Never switches to led2, led2 doesnt react. But touching both 'stroomjuist' with 5v increases 'Pulse'. Thanks for the tips!

Edit: touching them should increase them once, like the code says. It should switch to false. So you will need to touch them alternating.

There's also a problem with these two lines:-

switcheroo == true;
.
.
switcheroo == false;

Should be:-

switcheroo = true;
.
.
switcheroo = false;

Okay hey guys! Deadline is over, didn't work properly, but worked anyhow for just enough.

Okay, so one input never worked, i thought it was quite weird, i'm fairly certain it was the wiring or some other hardware error.

The analogRead pulsated, it went from 1023 to 0 in a few seconds on the serial monitor, and then going from 1023 tot 0 repeatedly. Whilst the other input went from 0 tot 29 quite steadily, with i think a 22k resistor.

This makes it fairly certain that it was indeed the wiring that wasn't functioning. But how i wonder, i demolished the whole project now. So i won't know for sure.