Pages: [1]   Go Down
Author Topic: Reseting a variable to 0  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. I've been having some difficulties with this issue and I can't find a good source to explain me what to do in this situation, so I decided to post it here as it has solved some other problems that I had in the past.

What I'm trying to do here is to make work a DC motor by pressing one of the keys of the computer keyboard (number 1), and turning it off with the same key. To do that, what I do is create a variable, and when I press the key, add 1 to that variable.
Now the variable is equal to 1.
Once I press again the same key on the pc computer, the variable should reset to 0 so the motor stops, but this is where I fail to program.

I know there's various options, like when the variable surpases a certain number reset it back to 0 (what I tried), but It doesn't seem to work.

I use Arduino UNO and Motor shield R3 if that helps. If you need anything else, just ask. Thank you!
Here's the code:

Code:
void setup()
{
  Serial.begin(9600);
 
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
 
  Serial.println("apreta 1 per anar endavant");
  Serial.println("apreta 2 per anar endarrere");
}
   
void loop()
{
  char a = 0;
    if (Serial.available() > 0)
  {
    a = Serial.read();
    if (a == '1')
       {(a++);
       if(a=2){
       a = 0;}
     }
       
  if (a=1) {     
       digitalWrite(12, LOW); //Establishes forward direction of Channel A
       digitalWrite(9, LOW);   //Disengage the Brake for Channel A
       analogWrite(3, 123);   //Spins the motor on Channel A at half speed
  }
  if (a=0) {
        digitalWrite(9, HIGH);
  }}}
Logged

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

Code:
if(a=2){
You got the first one right, but not this comparison
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.

Worst state in America
Offline Offline
God Member
*****
Karma: 24
Posts: 672
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(a=2){
You got the first one right, but not this comparison

If I had a dollar for every time I made that mistake.........
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1876
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or the next two. The quickest way to swap between 0 and 1 is to use

a=1-a; So if a == 0 you get 1 and if a == 1 you get 0.

Mark
Logged

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

Well I use
Code:
a = (a + 1) & 1;
the variable a will alternate between 0 and 1 every time this is executed
as it will if a is initially 0 or 1 with this code
Code:
a = a^1;
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (a == '1')
{
  (a++);
  if(a=2)
  {
     a = 0;
  }
}

There are a few things wrong with this part aside from the previously mentioned errors with the if statement. '1' and 1 are the not the same as far as the micro controller goes, so it would be best not to mix them like you've done here. If a is '1', then you increment it (you don't need the parenthesis surrounding a++ by the way) to '2'. Now you are comparing it to 2. 2 and '2' are not the same. So say that issue is also fixed, you then run into a logical error of what happens the next time you hit the button after your reset a to '0'?
Logged

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

Quote
If I had a dollar for every time I made that mistake...
If I had a pound for every time I had to point it out...
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.

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

Nothing, the motor keeps going. Sorry for being such a noob, I'm still learning smiley
How should the code look then?
Logged

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

Quote
Nothing,
So you changed the code and it won't work. So post what you have changed it to and we can see where you haven't understood what was said.
Logged

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

Okay, this is what I changed, but I'm not sure what you meant by caomparing the 2 and '2'

Code:
void loop()
{
  char a = 0;
    if (Serial.available() > 0)
  {
    a = Serial.read();
    if (a == '1')
    {a++;}     
    if (a > 1)
    {a == 0;
    }
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, this is what I changed, but I'm not sure what you meant by caomparing the 2 and '2'
First off, your coding style makes this very difficult to read. Use the Tools > Auto Format before posting your code to save our sanity.

2 and '2' are not equal. '2' is a character representation of 2 and has the equivalent decimal value of 50. When you fix the later mentioned error, you'll notice that if (a > 1) will always be true because anything you type into the serial monitor will have a binary value greater than 1. http://www.asciitable.com/

Code:
   a = Serial.read();
    if (a == '1')
    {
      a++;
    }      
    if (a > 1)
    {
      a == 0;
    }
You're mixing = and == again. If you want to set the value of the left side variable, use =. If you want to compare the value on the left side to the value on the right side, use ==.
« Last Edit: November 17, 2012, 01:16:42 pm by Arrch » Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or the next two. The quickest way to swap between 0 and 1 is to use

a=1-a; So if a == 0 you get 1 and if a == 1 you get 0.

Mark

I prefer
Code:
a = !a;
Logged

Pages: [1]   Go Up
Jump to: