Pages: [1]   Go Down
Author Topic: How to save state from a analogRead switch?  (Read 1786 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI!

ok, here goes....

I am working on a little project and now I am stuck.

I have a switch (stalk) that has 4 positions. each position gives different output voltage, which I monitor with analogRead...
Each switch position will do different action, I managed to get 4 leds light up one by one when different voltages (switch positions) are given.
after eack positoin switch returns to ''0''.
an this is where I stop - I need to save switch state... I mean, when I switch to ''do_action_1'' this action continues untill switch is moved to different position.

so far I have:
potentiometer
servo
stalk

Potentiometer gives some value and the servo does nothing untill switch 1st position is set.
switch is in position 1 and servo follows knob, when I release switch - servo stops, and I need servo to keep following knob untill different switch position is given.

I've been reading about booleans, states, bounces, debounces, but don`t get the idea whitch one to use....
 Thanks smiley-wink

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 636
Posts: 50277
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but don`t get the idea whitch one to use....
You read the switch state somewhere. You use some if statements to choose some block of code to execute.

It is up to you to keep doing this every pass through loop.

Without seeing your code, knowing what is does, and what you want it to do, instead, this is the best advice I can offer.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am making automotive cruise control. At least trying smiley-grin

Since I dont have signal generator (that is going to change soon) and I cant simulate tachometer signal,
 that is going to be used as input, I am bench testing the circuit with potentiometer as input.

Throttle will be controled with hobby servo (at least at the start)

and the stalk is oem cruise control stalk from a Lexus IS200.
This is the shematic of the switch:




As for the code, this is my first attemt of creating something with programming involved,
I am pretty good at hardware level and electronic, but software is giving me creeps smiley-grin

here it is so far:
Code:
#include <Servo.h>
Servo acc;
int servoPin = 9;
int stalkPin = 0;
int potPin = 1;

int val; //value from cruise stalk
int pos; //value from knob (later - tachometer)


void setup()
{
  acc.attach(servoPin);
  pinMode(stalkPin, INPUT);
  pinMode(potPin, INPUT);
  
  Serial.begin(9600);
}
void loop()
{
  val = analogRead(stalkPin);
  pos = analogRead(potPin);
  pos = map(pos, 0, 1023, 0, 179);

  Serial.println(val); //just to monitor what values are given by cruise stalk
  delay(10);
  
if (val > 940 && val < 980) //this is SET/COAST mode, this is where
 I need for servo to hold its position given by knob (later- tach) first to solve SET mode,
then coast mode.

{
  acc.write(pos); //need to set servo to hold its position and follow knob movement.
}

if (val > 990 && val < 1010) //this is RESUME/ACC function. Resume sounds difficult, so lets start with Accelerate
{
  acc.write(pos + 10); //servo position +10 values to open throttle a bit to give more juice.
}

if (val > 850 && val < 900) //this is CANCEL, that can be solved later.
{
  
}

if (val > 1018 && val < 1023) //this is ON/OFF switch. I think that should be at the start of the loop,
and also, need to remember previous state...
{
  
}
}


thats about it what I have now.
Thanks for any advices smiley-wink
« Last Edit: August 14, 2011, 03:00:00 pm by njakts » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 636
Posts: 50277
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pinMode(potPin, INPUT);
  pos = analogRead(potPin);
The pinMode function only affects digital pins. Are you connecting the pot to a digital pin or an analog pin?

So, you have some code. Some of the comments need to be split onto more than one line, so we don't need to scroll so much.

What does that code do that you don't want? What does that code not do that you do want?

What values are you seeing printed? Why do you not print the value from the map function (with some prefix so you know what the value being printed is?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as I said, this is my first code I am writing.

Both - stalk and knob are connected to analog inputs. What difference does it make? Whats wrong with setting pinMode?

values given by stalk are:
ON/OFF - 1023
SET/COAST - 962
RES/ACC - 1000
CANCEL - 886

they are floating a bit thats why I use (val > 940 && val < 980) for each function.

the stalk is like momentary switch. after each press it returns to ''0''
I need:

if (val > 940 && val < 980) //IF SET is pressed once, servo controlling with knob is activated, and continues endlesly untill cancel or on/off is pressed

{
  acc.write(pos);
}

so I need to monitor the previous state? I dont get this part.

''Why do you not print the value from the map function (with some prefix so you know what the value being printed is?''
this I dont understand either

english is not my native language, thats why I am trying my best for you to understand.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
as I said, this is my first code I am writing.
Then I would strongly recommend that you don't do this project. Controlling a car is a very safety critical thing to do. It is not the project you should start with. There are lots of things that can go wrong both with software and hardware. A car is a very hardware hostile environment, things can go wrong even if you know what you are doing.
There is a warning on the data sheet of the 328 processor not to use it for life dependent systems or automobile applications.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If we can put aside the dangerous project he is doing is it possible to get the solution to his problem? I am also doing a project that has more than one switch and each switch needs to control a new and different function when pressed. They are momentary switches so they go from high to low when pressed. I am running into the same problem as he is.

Thanks
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If we can put aside the dangerous project
A bit like saying

"Appart from that, how was the play, Mrs Lincon"
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 636
Posts: 50277
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Both - stalk and knob are connected to analog inputs. What difference does it make?
What difference does it make? Are you seriously asking that?

Quote
Whats wrong with setting pinMode?
Analog pins are, by definition, output only. There is no need to tell an analog pin that it is an output pin. As a result, the pinMode command applies only to digital pins. Setting the mode of digital pins 0 and 1, and using Serial at the same time, is generally not a good idea. Digital pins 0 and 1 ARE the hardware serial port.

Quote
the stalk is like momentary switch. after each press it returns to ''0''
Then, you need to store its value when it is not 0, and do not overwrite that value when it is 0.

Quote
so I need to monitor the previous state? I dont get this part.
Code:
int nonZeroVal = 0;

void loop()
{
   int val = analogRead(stalkPin);
   if(val > 0)
   {
     npnZeroVal = val;
   }

   if(nonZeroVal > 940 && nonZeroVal < 980)
   {
      // Move the servo
   }
   else // Remaining clauses...
}

Quote
''Why do you not print the value from the map function (with some prefix so you know what the value being printed is?''
this I dont understand either
You have Serial.println(val); in your code now. So, in the serial monitor, you see a series of numbers, one per line. What do those numbers mean?

If you add Serial.println(pos); to the code, now you see two series of numbers, mixed. Which is which? What do any of the numbers mean?

If you had:
Code:
Serial.print("stalk val: ");
Serial.println(val);
Serial.print("servo pos: ");
Serial.println(pos);
you would still have two series of values, mixed together, but you would know what each number meant.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Analog pins are, by definition, output only.
sp. "input"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 636
Posts: 50277
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sp. "input"
Yeah, yeah. Stupid fingers...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
as I said, this is my first code I am writing.
Then I would strongly recommend that you don't do this project. Controlling a car is a very safety critical thing to do. It is not the project you should start with. There are lots of things that can go wrong both with software and hardware. A car is a very hardware hostile environment, things can go wrong even if you know what you are doing.
There is a warning on the data sheet of the 328 processor not to use it for life dependent systems or automobile applications.

You are not first who says this to me and this is what I will tell you.
What are you doing when you are driving with oem cruise control on? are you sleeping? I guess no. Come on, there are brakes, there is shift lever, clutch and in the end you can turn off the engine when (if) something goes wrong. and there are two options - unstoppable acceleration (how can you miss that?) or cruise control not disengaging... what would you do? If you will wait and see what happens you could end up in a tree or school bus, but seriously?
and I WILL NOT put it it my car untill I am 100% sure it is working right.

I am going to Moscow tomorrow, so will take a week off this project. Hope to bring a signal generator and o-scope with me.

Thanks for your responses so far.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This I think will be my last comment in this thread:-
Quote
I WILL NOT put it it my car untill I am 100% sure it is working right.
I am sure you will not, but do you have the skills to know when it is safe? You might be 100% sure but do you have the knowledge / experience / test equipment for it actually to be 100% safe. I have been in electronics over 40 years, I would not attempt this project because I know some of what can go wrong, I know the testing I would have to do to make sure that the bits I know about would be safe. I don't know what special measures are also needed for automobile applications. Nor what tests are required to verify them.

The people that develop these work in large labs with a large budgets, masses of test equipment and a large bank of knowledge, experience and expertise.

One of the things I do know about is the testing you will have to do to prove the lack of susceptibility to electromagnetic interference. I don't have the equipment to do this and I suspect nether do you.
Lack of sufficient immunity could trigger faults, when say a motorcycle rides by.

I also don't know about what safety interlocks are required but I am dam sure some are required. Both in the hardware and software.

I am sure you would be very careful at first but remember most accidents with power tools happen not the first time they are used it is when you become familiar with them.

Quote
Come on, there are brakes ......
Yes, but it is often the surprise of something unexpected happening that prevents rapid corrective action, unfortunatly that is built into human beings. But it is not just you that is in danger, that would be fine (see what the Darwin Awards are all about), it is the danger to others, perhaps innocent bystanders that is the worry.

I did once drive to St Petersburg in 1990, I know the attitude to risk is quite different in your country. My biggest surprise was when two drunks riding one single bicycle pulled out in heavy fast moving traffic, right in front of me. If it wasn't a western car with an excellent breaking system I fear they would have been killed.
Logged

Pages: [1]   Go Up
Jump to: