IF and AND

hey guys, havnt been here in a long time.

I have a Duemilanove that i am playing with to control fan and a water pump for an intercooler.

I wont rave on about it, but i am testing this out on a bench with a pot (as a temp input) LED x 2 (as my fan and pump outputs, and a switch (which would be a pressure switch).

i uploaded the (if statement conditional) from the examples library.

i played with the values and could turn on one LED with the POT.

then i added some code for a second led and drove it with the pot HAPPY DAYS.
then i changed the code so i could turn on an LED once the POT had reached a set value and also once the switch had been closed. IE if pot > VALUE, AND switch HIGH, write LED HIGH. if only it was that easy.

so right now i cant work out how to use the AND function. this is the first time in years ive played with a micro (they just sit on my bench for months) and im becoming reminded as to why i avoid them like the plague.

if_2.ino (1.57 KB)

You've got a tiny amount of code, so why didn't you just post it, so that those of us on mobile devices can see it too?

I don't see any switch code or "and" function. Did you upload the right code?

/*
  Conditionals - If statement
 
 This example demonstrates the use of if() statements.
 It reads the state of a potentiometer (an analog input) and turns on an LED
 only if the LED goes above a certain threshold level. It prints the analog value
 regardless of the level.
 
 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 13 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 created 17 Jan 2009
 modified 9 Apr 2012
 by Tom Igoe

This example code is in the public domain.
 
http://arduino.cc/en/Tutorial/IfStatement
 
 */
 
// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}

I attached the code, im sure this site used to display attachments. hang on

// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
const int switchPin = 02; // input switch

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode (switchPin, INPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold, && switchPin HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);

}
// IVE TRIED THE FOLLOWING CODE IN PLACE OF WHAT IS ABOVE. ALL I GET IS ( expected')' before numeric constant/AND)
// if (analogValue > threshold, AND switchPin HIGH)
// if (analogValue > threshold, AND switchPin == HIGH)
// if ((analogValue > threshold), 'AND' (switchPin HIGH))
// if (analogValue > threshold, && switchPin HIGH)

// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}

Please go back and edit your post to include "code tags" so your code will be appropriately formatted. There are instructions for this in the sticky posts at the top of the forum.

What you are doing is referred to as "shotgunning". But you missed the barn this time. Have you considered consulting the reference, available in the help tab of the IDE?

I hate C. im giving up again for another year.

You're not alone. It's not really a good beginners language.

AWOL:
You've got a tiny amount of code, so why didn't you just post it, so that those of us on mobile devices can see it too?

AARG i went to the reference section, i couldnt find anything on AND, surely AND is used by you guys when you want multiple things to happen/line up before something else can happen?

aarg:
You're not alone. It's not really a good beginners language.

@f1111 : You will be alone if you use that word in your now-deleted post again.

f111:
AARG i went to the reference section, i couldnt find anything on AND, surely AND is used by you guys when you want multiple things to happen/line up before something else can happen?

??? && - Arduino Reference

oh yeah i saw that this morning in an earlier fit of rage. i tried various forms of the code shown but still get (expected ')' before numerical something something)

What is wrong with my code, how the hell am i supposed to use the AND function ? ive been pouring through the arduino cookbook, google, coffee grinds,,,,,, anything

it tells you exactly how to do exactly what you want in that link.

post me something from that link, and a section of your code, and tell me what you don't understand about it.

you were closest here:

if (analogValue > threshold, && switchPin HIGH)

this is correct

if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // read two switches
// ...
}

can you spot the differences??

ive been pouring through the arduino cookbook

Maybe you should try poring instead.

QDeathstar

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold, && switchPin HIGH) { <THIS BIT HERE fails during compile.
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);

// IVE TRIED THE FOLLOWING CODE IN PLACE OF WHAT IS ABOVE. ALL I GET IS ( expected')' before numeric constant/AND)
// if (analogValue > threshold, AND switchPin HIGH)
// if (analogValue > threshold, AND switchPin == HIGH)
// if ((analogValue > threshold), 'AND' (switchPin HIGH))
// if (analogValue > threshold, && switchPin HIGH)

if (analogValue > threshold, && switchPin HIGH) {

if (applejacks > 5 && digitalRead(3) == HIGH) {

What is wrong with my code, how the hell am i supposed to use the AND function ? ive been pouring through the arduino cookbook, google, coffee grinds,,,,,, anything

The fact that there are hundreds of thousands of programmers who know C and don't have trouble using a logical AND suggests you're doing something wrong in your search. First, there is no AND function...it is a logical operator, so perhaps that's where you should start looking.