First project idea, needing code help

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
chris48083
 
Posts: 2
Joined: Mon Feb 22, 2010 5:50 pm

First project idea, needing code help

Post by chris48083 »

This is mostly fluff:
So I just got my Arduino start pack in the mail this week. I went through all the tutorials, and have a basic understanding of programming/electronics, so went forth with my first project idea. Fairly simple - my thermostat only controls whether my heat is on or off (it's an old mercury thermostat, so the mercury actually connects two leads when the temperature is too low, thus switching on the heating). The idea is that I should be able to digitize this.

As far as the circuitry is concerned, I'm working at it as three different parts. There will be a control circuit (actually turning the heat on/off), a temperature reading circuit, and a user input circuit.

This is the actual problem:
Currently I'm just working on the user input. I've put together a simple circuit with two switches and two LEDs. I've also written a simple sketch, switch1 is pushed, led1 turns on for one second and the same for switch2/led2 respectively. In the final project the code shouldn't differ too much, except adding lines to increase and decrease a setTemp variable (and maybe outputting to a small LED screen? Let's get the buttons working first...). Below is the code:

Code: Select all

/* Simple code to test a two switch/two LED setup.  When switch1Pin is HIGH, led1Pin should light up for one second.  Same for switch2Pin and led2Pin respectively. */

int led1Pin = 8;
int led2Pin = 9;
int switch1Pin = 1;
int Switch2Pin = 2;

int switch1state;
int switch2state;
int val;
int val2;
int val3;
int val4;

void setup() {
  Serial.begin(9600);
  Serial.print("Setup...");
  
  pinMode(led1Pin,OUTPUT);
  pinMode(led2Pin,OUTPUT);
  
  pinMode(switch1Pin,INPUT);
  pinMode(switch2Pin,INPUT);
  
  switch1state = digitalRead(switch1Pin);
  switch2state = digitalRead(switch2Pin);
  

  Serial.println("...complete.");
}

void loop() {
  val = digitalRead(switch1Pin);
  val3 = digitalRead(switch2Pin);
  delay(10);
  val2 = digitalRead(switch1Pin);
  val4 = digitalRead(switch2Pin);
  
  if (val == val2) {
    if (val != switch1state) {
      if (val == HIGH) {
        digitalWrite(led1Pin,HIGH);
        delay(1000);
        digitalWrite(led1Pin,LOW);
        Serial.println("Switch 1");
      }
    }
  }
  if (val3 == val4) {
    if (val3 != switch2state) {
      if (val3 == HIGH)
      digitalWrite(led2Pin,HIGH);
      delay(1000);
      digitalWrite(led2Pin,LOW);
      Serial.println("Switch 2");
    }
  }
  switch1state = val;
  switch2state = val3;
}
  

When I compile this code I get the fallowing error message:

Code: Select all

error: redefinition of 'int val' In function 'void setup()':
 In function 'void loop()':
In an effort to keep troubleshooting to a minimum I've added a few lines to output what the program is doing, but I don't think that has any effect on the error message that I am receiving? Help??

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: First project idea, needing code help

Post by mike31416 »

Case matter with variable names. You have int Switch2Pin = 2; (S is uppercase) but use a lowercase 's' in pinMode(switch2Pin,INPUT);

Not sure why you are getting the error message you see. I got:

In function 'void setup()':
error: 'switch2Pin' was not declared in this scope In function 'void loop()':


Mike

chris48083
 
Posts: 2
Joined: Mon Feb 22, 2010 5:50 pm

Re: First project idea, needing code help

Post by chris48083 »

I fixed the case issue, but I don't think that was causing the error message (but it would have later, thanks for the catch).

I decided the code was short enough to just retype it in and compile after each new line or section. It worked, compiling just fine, but I have no idea what's different, I can't find any difference? Here's the new code, thanks for the help guys.

Code: Select all

int led1Pin = 8;
int led2Pin = 9;
int switch1Pin = 1;
int switch2Pin = 2;

int switch1state;
int switch2state;
int val;
int val2;
int val3;
int val4;

void setup() {
  Serial.begin(9600);
  Serial.print("Setup...");
  
  pinMode(led1Pin,OUTPUT);
  pinMode(led2Pin,OUTPUT);
  
  pinMode(switch1Pin,INPUT);
  pinMode(switch2Pin,INPUT);
  
  switch1state = digitalRead(switch1Pin);
  switch2state = digitalRead(switch2Pin);

  Serial.println("...complete");
}

void loop() {
  val = digitalRead(switch1Pin);
  val3 = digitalRead(switch2Pin);
  delay(10);
  val2 = digitalRead(switch1Pin);
  val4 = digitalRead(switch2Pin);

if (val == val2) {
  if (val != switch1state) {
    if (val == HIGH) {
      digitalWrite(led1Pin,HIGH);
      delay(1000);
      digitalWrite(led1Pin,LOW);
      Serial.println("Switch 1");
    }
  }
}
if (val3 == val4) {
  if (val3 != switch2state) {
    if (val3 == HIGH) {
    digitalWrite(led2Pin,HIGH);
    delay(1000);
    digitalWrite(led2Pin,LOW);
    Serial.println("Switch 2");
    }
  }
}
switch1state = val;
switch2state = val3;
}

User avatar
lewis
 
Posts: 4
Joined: Sun Aug 30, 2009 1:54 pm

Re: First project idea, needing code help

Post by lewis »

In your original code it looks like you were missing a pair of curly brackets for if (val3 == HIGH). I don't have the Arduino IDE currently installed so I can't check if that was causing the problem.

Also I would recommend using better variable names. Having 4 all called val will get very confusing.

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: First project idea, needing code help

Post by mike31416 »

The missing braces would not cause the syntax error. Without the braces it would just execute the single line "digitalWrite(led2Pin,HIGH);" if val3 were high. The remaining three statements would always execute regardless of the value of val3.

User avatar
lewis
 
Posts: 4
Joined: Sun Aug 30, 2009 1:54 pm

Re: First project idea, needing code help

Post by lewis »

You are correct that it shouldn't cause an error, but it does change the expected nature of the code which could lead to errors.

If you get rid of the obvious first then it's easier to find the obscure.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: First project idea, needing code help

Post by adafruit_support_bill »

Compiling your first example (version 17 of the IDE), I get the error:
In function 'void setup()':
error: 'switch2Pin' was not declared in this scope In function 'void loop()':
Fixing the case in the declaration of switch2Pin it compiles clean. As Lewis pointed out, the only other syntactical difference between the two pieces of code is the missing curly-brace.

chris48083
 
Posts: 2
Joined: Mon Feb 22, 2010 5:50 pm

Re: First project idea, needing code help

Post by chris48083 »

First off, thanks for all the help guys, I'm having a ton of fun with this. So thanks for taking the time to help a noob out! :D

I got the first program to compile and run, it didn't seem to be operating correctly, so I wrote another sketch to just test the circuit. I kept getting false readings on my switches, so I also simplified the circuit so that it's just a really simple in/out kind of deal. I'm still getting false readings however. Here's what I've got.

Code: Select all

int switch1pin = 2;
int switch2pin = 3;
int led1pin = 8;
int led2pin = 9;

int switch1state;
int switch2state;
int switch1;
int switch2;

void setup() {
  Serial.begin(9600);
  Serial.print("Setup...");
  
  pinMode(led1pin,OUTPUT);
  pinMode(led2pin,OUTPUT);
  
  pinMode(switch1pin,INPUT);
  pinMode(switch2pin,INPUT);
  
  Serial.println("...complete");
  Serial.println("Diag...");
  Serial.println(" ");
  
  Serial.println("Switch power on in 5 sec.");
  delay(5000);
  switch1state = digitalRead(switch1pin);
  switch2state = digitalRead(switch2pin);
  Serial.println("Switch Status:");
  Serial.print("switch1state = ");
  Serial.println(switch1state);
  Serial.print("switch2state = ");
  Serial.println(switch2state);
  Serial.println(" ");
  delay(1000);
  
  Serial.println("LED power on in 5 sec.");
  delay(5000);
  digitalWrite(led1pin,HIGH);
  delay(1000);
  digitalWrite(led2pin,HIGH);
  Serial.println("LED power on.");
  delay(1000);
  Serial.println("LED power off in 5 sec.");
  delay(5000);
  digitalWrite(led1pin,LOW);
  delay(1000);
  digitalWrite(led2pin,LOW);
  Serial.println(" ");
  
  Serial.println("Diag Complete.");
}

void loop(){}

  
  
I wrote all the delays in to give me a chance to watch the breadboard.

And schematic-
Image
It was my first time using this software, I couldn't figure out how to edit the pin numbers, but you can assume they are in the correct places.

Serial monitor read out-

Code: Select all

Setup......complete
Diag...
 
Switch power on in 5 sec.
Switch Status:
switch1state = 0
switch2state = 1
 
LED power on in 5 sec.
LED power on.
LED power off in 5 sec.
 
Diag Complete.
Both switches are not being pushed. I don't understand why switch2state is coming out as 1. To further trouble shoot, I unplugged both switches completely, so nothing is plugged into switch1pin or switch2pin at all (should read '0,' no matter what? right?). Here's the serial read out of doing that-

Code: Select all

Diag...
 
Switch power on in 5 sec.
Switch Status:
switch1state = 1
switch2state = 1
 
LED power on in 5 sec.
LED power on.
LED power off in 5 sec.
 
Diag Complete.
Needless to say I'm pretty baffled. If there's no physical way any voltage could reach the pins, then how could the be reading a value of 1?? :roll:

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”