Jump to content
43oh

Problem in reading switch values


Recommended Posts

Hi. First of all I would like to thank developers and supporters of Energia as well as 43oh.com for their wonderful efforts.

 

I have worked on Arduino for quite a bit of time now and am now trying on the MSP430, of course using the launchpad.

Recently, I wrote a code in Arduino which reads the value of 4 push-to-on switches and positions the servo according to the switch state.

 

For example, following cases happen : if no switch is pressed, the servo remains at 0, if two switches are pressed, servo moves to say 30 degrees and so on. For this, I have used switch-case control, switching the total sum of switch states, which were stored in an integer array.

 

Now the problem is that this code works perfectly with Arduino, but when tried with Energia on MSP430G2553, the servo oscillates between 2 or three values randomly and never gets a fixed position. The serial monitor shows different values for example, 2, 3, 3, 4, 2, 3,...... and the servo will oscillate between these values according to the switch case. This all happens irrespective of the switch states.

 

I think my code is fine as it works perfectly in Arduino and i have checked all the pin assignments and the connections a million times but nothing gets this to work. May be I am missing something in the transition from Arduino to MSP430 or something else is wrong.

 

Please correct me where I am wrong. Also if anyone thinks this approach is not good enough and has a better idea is welcome to share.

 

Thanks

Smat

// servo control using switches at 3 4 5 6 for 

#include <Servo.h> 

int sen[]={3,4,5,6}; //switch connections

Servo myservo;  //servo object
 
int pos = 0;    // variable to store the servo position 
unsigned int sum;
int f, r, t, m;
int load[]={f, r, t, m};  // array to store switch values 0 or 1

void setup() 
{ 
  for(int i= 0; i< 4; i++)
    {
      pinMode(sen[i], INPUT); // declare pushbutton as input
      digitalWrite(sen[i],HIGH);       // internal pull up enable
      Serial.begin(9600);
    }
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  sum=0;        // int vairable to store the sum of load array
  for(int j=0; j<4; j++)
  {
    
    load[j]=digitalRead(sen[j]);
    sum = sum  + load[j];
    delay(30); 

  }
  Serial.print(sum);
  Serial.println();
  
switch(sum)
 {
   case 0:
   myservo.write(100);
   //delay(2000);
   break;
   
   case 1:
   myservo.write(75);
   //delay(2000);
   break;
   
   case 2:
   myservo.write(50);
   //delay(2000);
   break;
   
   case 3:
   myservo.write(25);
   //delay(2000);
   break;
   
   case 4:
   myservo.write(0);
   //delay(2000);
   break;
   
  //default:
  //delay(500);
}
} 

 

 

  

post-30512-0-98906500-1356794015_thumb.jpg

Link to post
Share on other sites

I don't use energia so I'm not too familiar with its ins and outs. But what I did notice is in your void setup() routine you loop but inside it you use pinmode(Sen......) not pinmode(Sen.........)

 

So you could easily be reading floating pins as this is where you set pullups.

 

Sent from my GT-I8160 using Tapatalk 2

 

 

Link to post
Share on other sites

You might try and disconnect the RX/TX jumpers from J3. Pin 3 and 4 are the serial UART pins.  They may be adversely affecting you.

 

...

wait

...

 

you are using the Serial port and then trying to use those as switches?

 

...

wait

...

Try and use Serial.begin only once. You have it in a loop.

 

This page illustrates pins with special usage:

 

https://github.com/energia/Energia/wiki/Hardware#wiki-LaunchPad_MSP430G2553

 

-rick

Link to post
Share on other sites

You might try and disconnect the RX/TX jumpers from J3. Pin 3 and 4 are the serial UART pins.  They may be adversely affecting you.

 

...

wait

...

 

you are using the Serial port and then trying to use those as switches?

 

...

wait

...

Try and use Serial.begin only once. You have it in a loop.

 

This page illustrates pins with special usage:

 

https://github.com/energia/Energia/wiki/Hardware#wiki-LaunchPad_MSP430G2553

 

-rick

hi. i tried using different pins other than 1,1  and 1,2 but no use. i even disabled the serial print and removed the TXD RXD jumpers. the servo still keeps on oscillating between 2 or 3 positions irrespective of the switch state.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...