Help with code and wiring involving 3 position switch & dc motor

I am working on a project with arduino uno that uses a temp sensor to turn on a motor once it goes above a certain temp.

So far, I have set digital pin 3 as an output and using "if" when the temp gets above a certain number(a number i set manually at the moment) it will digtalWrite 3 HIGH, which turns the motor on. So far so good.

Now I want to add a 3 position off-on-on slide switch to power the arduino once I make it portable. When it is off I want it to obviously turn everything off. In the other two on positions I would like it to do the following.

On postion 1...all it needs to do is read the numbers (b/w 0-1023) that are coming in which I believe it is already doing without a switch now??

On position 2...when in this position I would like the arduino to take the last number it read from "on position 1" (so write before the switch gets moved to position 2)and use that number as the new max number. So anything above that number will turn on the motor. It will not be a number I enter manually anymore.

Not sure if I would use if or if...while...or perhaps use millis somehow??

I am also not too sure how to wire it properly either. Would I use 2 more digital pins (one for each on postion)?

Any help would be great...thanks

On postion 1...all it needs to do is read the numbers (b/w 0-1023) that are coming in which I believe it is already doing without a switch now??

Coming in from where? The temperature sensor?

On position 2...when in this position I would like the arduino to take the last number it read from "on position 1"

I think you need to post a picture of this switch, and a schematic. How many legs does it have? To provide the capability you describe, it would need at least 4.

A digital pin (like D3 which you mention) is simply on or off. For Temperature measurement, you want to use an analog pin to get the 0- 1023 resolution you mentioned. How I understand it is this. You want a slide switch with 3 positions:

  1. everything OFF
  2. Normal operation of thermostat, switching the motor on when a maximum threshold has been reached.
  3. record the current thermostat setting (say in an EEPROM) and using that value as the new threshold value.

Is that about it ?

Usually, a motor is not driven directly from a pin due to its power requirement.

from the temp sensor yes.

Here is the switch. I could not find an off-on-on anywhere so I too this one from a hair dryer.

switch2.JPG

switch.JPG

6v6gt...

I am using a transistor to run the motor unless there is a better way for a newbie like me to do it.

Your interpretation is close but not quite.

1.everything off 2.read the numbers from the temp sensor but do not turn anything on. Like I said I believe this is happening already based on what I have set up so far. It's basically having a way to "set" the max number here and keep the last number in memory when it is switched to position 3. 3.use the last number it was reading from position 2 right when the switch was flipped to position 3 and turn on the motor if above that.

I have it doing that now except I have to tell it what number to use as max. I want it to use the last number before the switch is flipped to position 3.

Is that more clear?

You may have trouble with that switch. The first thing you have to do is work out which terminals are connected to which others, in each position of the switch.

You could simplify the whole thing as follows, where the switch simply acts to pull 1 pin low if switched 'On' :

(a) Everything off: simply switch of the Arduino at its power supply. (b) Arduino power up with switch in 'Off' position: Your position (2) simply reading the temperature and not doing anything. (c) Switch moved to 'On' position after the Arduino has been powered up: Record the temperature sensor reading and store it in the EEPROM. Operate motor whenever the temperature threshold has been reached. (d) Switch already in 'On' position at Arduino power up: Operate motor whenever the temperature threshold (which has been stored in the EEPROM) has been reached.

As for the switch configuration, the ends of the wires are still attached so maybe this will help determine how it works? 1.The pin without wire is the off position, 2.the 2nd row of 2 pins was the first speed on hairdryer, are connected to each other and had a red wire. 3.the 3rd row of 2 pins was the next speed up on the hairdryer, are connected to each other and had a white wire. 4.the 4th row of pins are connected to each other and was a black wire.

So i am guessing the white wire was ground so in position 2 it was red(power) and white(gnd) and position 3 it was white(gnd) and black(power)??

I was going to start by getting it wired in series with the battery that will power the arduino. Then I thought I could wire power from pin row 2 on the switch maybe into say digital pin 4, then wire power from pin row 4(assuming black was power) to digital pin 5 on arduino. Setting both pins as an INPUT. The motor is connected to digital pin 3 using a transistor and is set as an OUTPUT.

Then I thought maybe I could write code saying what I want it to do...This coding will not be correct but you should get the idea of what I am trying to do/say.

first i already have this set up and it works

int temppin = 0; int tempreading;

void setup(0) pinMode(3,OUTPUT);

void loop() tempreading=analogRead(temppin);

if (tempreading > 765) //just picking a number for now digitalWrite(3,HIGH)

So then I thought somehow I could do the following

-define switch position 2 as say switchpos2. -".............................3...........switchpos3. -define what the last temp reading was in position 2 as switchpos2lastreading = the last tempReading while in switchpos2 //not too sure how to set this up.

then say... if(5, HIGH) && tempReading > switchpos2lastreading //pin 5 would only be HIGH if switch in postion 3. digitalWrite(3, HIGH);

else digitalWrite(3,LOW);

This is probably confusing and incorrect but I thought I would try to get what was in my head out so maybe it will help you guys help me. lol

anybody know if something along these lines would work??? Or another way that would work better???

thanks

Is this how your switch is configured (view from underside) ?

For example, in position 1, pins 1 & 2 are connected together and pins 5 & 6 are connected together.

Position 1   

1  2  3  4
|  |  |  |
====
====
|  |  |  |
5  6  7  8



Position 2

1  2  3  4
|  |  |  |
   ====
   ====
|  |  |  |
5  6  7  8


Position 3

1  2  3  4
|  |  |  |
      ====
      ====
|  |  |  |
5  6  7  8

I am not too sure without taking it apart which I could do I was just afraid I may not get it back together again. It wasn’t easy to find. I was hoping the way it was wired in the hairdryer would shed some light on that. I left the wires as is where they were attached at the pins (see pic below which is upside down relative to your drawing I think)because I believe the hairdryer was functioning in a similar way I would want it to. I am not sure how it is wired internally but whoever wired it for the hairdryer has pin 2 attached to pin 6 then a red wire out…3 attached to 7 then a white wire out…and 4 attached to 8 with a black wire going out of pin 4(opposite side of other wires). Am I right in assuming the white would be ground since it would be in play in both “on” positions? The red is power in position 2 and the black was power in position 3?? Sorry if this seems elementary but I am quite clueless with this stuff.

switch.JPG

There is no need to take it apart and there is no need to unsolder the bridged connections, but a battery and LED (if you haven't got a multimeter) could help determine how it is configured.

Ok I will play with the switch tonight and figure it out. What are your thoughts about wiring it to the digital pins as INPUTs (or would they be OUTPUTs?)and using code something like I am describing? Based on what I have had success with before I think it might work I just don't know how I can "define" the "last reading while in position 2" so I can tell it to use that # while in position 3.

So...once I figure out the switch config,

1.Do you think this coding and wiring or something similar would work? 2.When in switch position 3 do you have any ideas how I can tell it to pull that last number from when the switch was in position 2?

Thanks again for your help.

Cut the existing bridges on the switch between 2 - 6, 3 - 7 and 4 - 8 Warning: if opposite switch connectors (eg 1 & 5, 2 & 6 etc. are connected together internally, this will not work) Test the switch configuration to check it behaves as in the diagram below.

Now connect the switch Switch connector 2 connects to Switch connector 4 (bridge on switch) Switch connector 2 connects to Arduino power jack (*depending on battery voltage) Battery positive (+) connects to Switch connector 3 Battery negative (-) connects to Arduino ground

Test the switch now switches the Arduino off in position 1, but on in positions 2 and 3.

If test is OK, connect the switch as follows:

Switch connector 7 connects to Arduino ground Switch connector 6 connects to Arduino pin 4 via 1k resistor Switch connector 8 connects to Arduino pin 5 via 1k resistor

create a small sketch to test that the Arduino pins 4 and 5 behave as expected: hint: use pinMode( XXXXX, INPUT_PULLUP ) and note that this is different to your current sketch. In switch position 2 check Arduino pin 4 is LOW and Arduino pin 5 is HIGH In switch position 3 check Arduino pin 4 is HIGH and Arduino pin 5 is LOW

Next. Develop your software.

If the switch changes from 2 to 3 during the exection of the program Arduino pin 4 is HIGH and Arduino pin 5 is LOW. You can check for this and store the current value of tempReading in variable switchpos2lastreading and later in the EEPROM.

Position 1  

1  2  3  4
|  |  |  |
====
====
|  |  |  |
5  6  7  8



Position 2

1  2  3  4
|  |  |  |
   ====
   ====
|  |  |  |
5  6  7  8


Position 3

1  2  3  4
|  |  |  |
      ====
      ====
|  |  |  |
5  6  7  8

Ok I will try this. A few things have me confused though.

1.Why a 1k resistor vs a 10k resistor (suggests 10k in reference section)?

2.To test the switch's current configuration (after I cut the bridges) which pins to I connect the battery and meter too in position 2 and 3?

3.You say depending on battery voltage for connecting to Arduino power, isn't anything 12v or less ok?

4.Is using INPUT_PULLUP just a way to save me from connecting an external resistor?

5.In switch position 2 wouldnt arduino pin 4 actually be HIGH since the switch would be closed? and in position 3 arduino pin 5 would be HIGH? You show them the other way around. I must be missing something.

6.What code would I use to check for the switch change and store the value in tempReading

thanks again and sorry for all the questions

Answers in bold in your text:

homer2121: Ok I will try this. A few things have me confused though.

1.Why a 1k resistor vs a 10k resistor (suggests 10k in reference section)? 1K is a series resistor to prevent a wiring fault damaging the Arduino pin. 10 k is usually a pull up /pull down resistor to give a pin a clear state.

2.To test the switch's current configuration (after I cut the bridges) which pins to I connect the battery and meter too in position 2 and 3?

Position 1 The following should be connected: 1 to 2 and 5 to 6. No other combinations are allowed Position 2 The following should be connected: 2 to 3 and 6 to 7. No other combinations are allowed Position 3 The following should be connected: 3 to 4 and 7 to 8. No other combinations are allowed

3.You say depending on battery voltage for connecting to Arduino power, isn't anything 12v or less ok? 7 to 12+ volts must go through the regulator so use the power jack. A battery of less than 5.5 volts is connected to Vin on the Arduino.

4.Is using INPUT_PULLUP just a way to save me from connecting an external resistor? Yes

5.In switch position 2 wouldnt arduino pin 4 actually be HIGH since the switch would be closed? and in position 3 arduino pin 5 would be HIGH? You show them the other way around. I must be missing something. The switch switches the pin to ground, not to + so it is LOW when switched. This is standard with a pull up resistor.

6.What code would I use to check for the switch change and store the value in tempReading tempReading = AnalogRead( analog pin XXXX ) ;

thanks again and sorry for all the questions

Ok so I've got the switch wired in and it operates how I want it to. Now that I have got that tackled how do I do the following?

"create a small sketch to test that the Arduino pins 4 and 5 behave as expected: hint: use pinMode( XXXXX, INPUT_PULLUP ) and note that this is different to your current sketch. In switch position 2 check Arduino pin 4 is LOW and Arduino pin 5 is HIGH In switch position 3 check Arduino pin 4 is HIGH and Arduino pin 5 is LOW"

1.I have switch connector 6 and 8 going to the arduino digital pins 4 and 5 and I wired on a 1k resistor to each. If I am using INPUT_PULLUP do I still need to have these 1k resistors wired on?

  1. I have used pinMode(4,INPUT_PULLUP); and same thing for pin5. How do I check if Arduino pin 4 is LOW and 5 HIGH in switch position 2 etc? Do I use digitalWrite or how do I determine that? What would that code look like?
  1. Yes you need the following 1K resistors: Switch connector 6 connects to Arduino pin 4 via 1k resistor Switch connector 8 connects to Arduino pin 5 via 1k resistor These are not Pullup Resistors, they are to protect the Arduino pins against faults like writing HIGH to the pin when the pin is grounded etc.

  2. You can check the status of a digital pin with yyyy = digitalRead( PIN-NUMBER XXX ) ; You can output the results with the Serial.println( ) function. You'll see a 0 for LOW and a 1 for HIGH on the serial console.

Ok I was pretty sure I did everything right but in switch pos 2 pin 4 is HIGH and pin 5 is LOW...and in switch pos 3 pin 4 is LOW and pin 5 is HIGH. Do I need to change this around or does it really matter since each pin is changing this way too? I should still be able to check for this change and store the value that I will use as a threshold correct??

I am still not quite following you on how I tell it when the switch changes from 2 to 3 I want it to store the last reading from switch pos 2 and use it as the max # in switch position 3. Then while its in switch position 3 if the temp reading goes higher than the last reading from position 2, digitalWrite(3,HIGH);

Can you help me sort this code out? I haven't bothered writing the "if" statement until I figure out what coding to use to tell it to look for this switch position change and use the last reading from position 2.

int tempPin = 0;
int tempReading;
int switchpos2lastreading;
int switchpos3reading;


void setup() {
  Serial.begin(9600);
  pinMode(3,OUTPUT);// temp sensor
  pinMode(4,INPUT_PULLUP);//currently shows pin 4 HIGH in pos 2 and LOW in pos 3
  pinMode(5,INPUT_PULLUP);//currently shows pin 5 LOW in pos 2 and HIGH in pos 3 
  // put your setup code here, to run once:
}

void loop() {
  tempReading = analogRead(tempPin);
  Serial.print("Temperature = ");
  Serial.print(tempReading);
  switchpos2lastreading = analogRead(tempPin) ;
  
  
// put your main code here, to run repeatedly:
 delay(2000);
}/code]

Is there a function that would return the most return the most recent/last data from a previous condition or how would you tell it to use the number from switch position 2?

Thanks