smat 0 Posted December 29, 2012 Share Posted December 29, 2012 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); } } Quote Link to post Share on other sites
DanAndDusty 62 Posted December 29, 2012 Share Posted December 29, 2012 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 Quote Link to post Share on other sites
Rickta59 589 Posted December 29, 2012 Share Posted December 29, 2012 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 Quote Link to post Share on other sites
smat 0 Posted December 29, 2012 Author Share Posted December 29, 2012 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. Quote Link to post Share on other sites
smat 0 Posted December 29, 2012 Author Share Posted December 29, 2012 Ok guys, got it to work. Just replaced pinMode(sen, INPUT) with pinMode(sen, INPUT_PULLUP) and deleted the separate pullup line. Thanks for all your help. Cheers smat Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.