Grounding problem

I am working on a control panel for a monitor, using a arduino mega with a ethernet shield, the monitor is tcp commands are attached I have the program working , its basically the arduino attached to a rotary switch and sending out tcp commands depending on the position iof the switch, so I am using input_pullups and polling for lows, but it works fine when hook up to the usb port as soon as i go on the 9v power supply it just goes crazy sending comands at random almost like the boards ground is floating, is i remove the ground pin connected to the board and touch it with the outside of the usb plug it works again,

#include <SPI.h>
#include <Ethernet.h>


// mac adress (made up number)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x9E, 0x16 };
// Static IP for arduino
byte ip[] = { 192,168,0,222 };
// Static IP for Smartview
byte bmip[] = { 192,168,0,2 };
// Client Ethernet
EthernetClient clientSmartView;
// not needed 
String code;
int i;


/**** one time running code ****/
void setup() {
//pin setup
  pinMode(30, INPUT_PULLUP);
  pinMode(31, INPUT_PULLUP);
  pinMode(32, INPUT_PULLUP);
  pinMode(13, OUTPUT); 
 
  // serial connection for troubleshoot
    Serial.begin(9600);
    Serial.println("Serial connection established");
  delay(500);
    Serial.println("Port set at 9600 bps");
  delay(500);
// Connection Ethernet
  Ethernet.begin(mac,ip);
  delay(500);
  Serial.println("IP connection in progress ...");
  delay(1000);
  //if connected say so otherwise say failed
   if(clientSmartView.connect(bmip, 9992 )) {
   Serial.println("connection established");
   }
  else {
       Serial.println("Connection failed");
       }
  }

//main progran loop
void loop(){
//pin declare  
  
  int sensorVal1 = digitalRead(30);
  int sensorVal2 = digitalRead(31);
  int sensorVal3 = digitalRead(32);

 if (sensorVal1 != HIGH && i != 6) {
    delay(100);
    //Serial.println("pin 30");
    //Serial.println(i);
    MONAPIC();
    //int i = 6;
  } 
  if (sensorVal2 != HIGH && i != 7) {
   delay(100);
    
    //Serial.println("pin 31");
    //Serial.println(i);
    MONAVEC();
   // int i = 7;
  }
if (sensorVal3 != HIGH && i != 8) {
    delay(100);
   
    //Serial.println("pin 32");
    //Serial.println(i);
    MONAWAV();

   // int i = 8;
  } 
}
//subroutines
int MONAPIC()//monitor A picture
{
clientSmartView.connect(bmip, 9992);
delay(100);
if (clientSmartView.connected()) {
  clientSmartView.println("MONITOR A: ");
  clientSmartView.println("ScopeMode: Picture");
  clientSmartView.write(0x0A);
  clientSmartView.stop();
  i=6;
 }
return i;
}
int MONAVEC()//monitor A vector
{
clientSmartView.connect(bmip, 9992);
delay(100);
if (clientSmartView.connected()) {
  clientSmartView.println("MONITOR A: ");
  clientSmartView.println("ScopeMode: Vector100");
  clientSmartView.write(0x0A);
  clientSmartView.stop();
  i=7;
 }
return i;
}
int MONAWAV()//monitor A waveform
{
clientSmartView.connect(bmip, 9992);
delay(100);
if (clientSmartView.connected()) {
  clientSmartView.println("MONITOR A: ");
  clientSmartView.println("ScopeMode: WaveformLuma");
  clientSmartView.write(0x0A);
  clientSmartView.stop();
  i=8;
 }
return i;
}

SmartView Manual.pdf (2.78 MB)

as i go on the 9v power supply

What kind of 9V power supply? Is it red?

apparently i failed to read the reviews,

I can't comment on the suitability of that power supply, although the 650mA rating is pretty low for an ethernet shield toting Arduino.

I can comment on your code, though.

 if (sensorVal1 != HIGH && i != 6) {

The name sensorVal1 suggests to me that the variable contains a sensor value, and that it is one of many. An array might be better, but since there are only three, it's not really necessary.

The name i suggests that this is a throwaway when done variable, like a loop index. It is a horrid name for a global variable.

// not needed 
String code;

Then why not delete it, and make your program smaller?

   if(clientSmartView.connect(bmip, 9992 )) {
   Serial.println("connection established");
   }
  else {
       Serial.println("Connection failed");
       }
  }

Consistent
indenting
is
preferred.

Use Tools + Auto Format.

There are far more delay()s in setup() than are needed. In reality, NONE is the correct number to use.

    MONAPIC();

By convention, all capital letter names are reserved for constants. Functions are hardly constants. A function name should describe what the function does, like digitalRead(), pinMode(), Ehternet.begin(), etc. MONAPIC doesn't suggest anything to me. Looks like it could just as well have been called Jt52lhM().

Yes a co-worker who programs PIC chips as a hobby pointed out all my programming faux paus, I put it togheter from other examples and from what i remember from school (15 yrs ago), at any rate changing the power supply did the trick, I guess there is no regulation on the board itself.

claudio_araoz:
Yes a co-worker who programs PIC chips as a hobby pointed out all my programming faux paus, I put it togheter from other examples and from what i remember from school (15 yrs ago), at any rate changing the power supply did the trick, I guess there is no regulation on the board itself.

There is in fact a 5V regulator on the board. There has to be, since you can't run AVR chips higher than about 5.5V. It's just that your power supply was a piece of junk and the regulator couldn't handle it.