Check out my music: https://soundcloud.com/playinmyblues

Wednesday 7 June 2017

Raspberry Pi Zero W and Arduino Robot, June 7, 2017

Here is a quick post to show the current state of my robot. It is currently to the point where I have all the necessary components except maybe the transistors necessary to make H-bridge motor drivers for each wheel. I have transistors that might work but have not built an H-bridge to test them. I am using geared motors, a USB Boarduino, an RPi Zero W, some mini solderless breadboards, a powered USB hub, three UBEC's that supply 5V, 3A, some photocells, an 8-cell AA battery pack with rechargeables and a homemade chassis from plywood.

The photo shows everything running from AC/DC adaptors at moment. However, everything has been tested with homemade USB power cables. I just hope I do not make the mistake of plugging in the positive and negative of those USB cables into the same connection. I saw a recent product that offered the same power from a USB port that ran the power connections out to red and black shrouded alligator clips. The tweet said an accident waiting to happen!

Current state:

At the moment, with the programs I am running, I can send a number from the RPi Zero W by USB to the Arduino and have the Arduino light up and LED that number of times. The next stage is to use directional controls commonly used in video games, W, A, D, and S, to light up the LED's. I would also like to be able to adjust the brightness according to how long I hold down the directional key using PWM on the Arduino.

That way I know the current setup is ready to accept a more fully developed program that will accept both directional controls using Python on the RPi Zero W as well as using the photocell light sensors. Keep in mind, PySerial is used.
After finding a number of different tutorials on using PySerial with an Arduino, here is the one I found the most useful (easiest to use and mod to my purposes):
http://www.instructables.com/id/Interface-Python-and-Arduino-with-pySerial/

My apologies if the code does not copy correctly. I copied and pasted the code into Blogger then edited it where needed. Here are the two programs I am using to control the LED's at this time:


Python code:
# program: control_arduino_leds.py
# This program sends data from the RPi over USB to an Arduino and receives
# that data back - repeated. The data sent to the Arduino controls the number
# of times the LED's light up on the Arduino.
# The pair program for the Arduino is receive_from_pi_light_leds.ino
from time import sleep
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # Establish the connection on a
# specific port

counter = 7
# testing number, 48 is the character 0, zero, 57 is 9
# Below 32 everything in ASCII is gibberish
try:
  while True:
    ser.write(str(chr(counter))) # convert the decimal number to ASCII then 
                                 # send it to the Arduino
    print "counter = %d" % counter
    print ser.readline() # read the newest output from the Arduino
    sleep(0.5) # delay from 1/10 seconds
    if counter == 255:
    counter = 32

finally:
  ser.close() 

Arduino Sketch:
// program: receive_from_pi_light_leds.ino
/* This program receives data from the RPi over USB and sends data back. Before
 * sends the data back, it calls a function to light one or more LED's according
 * to the data received. 
 * PAIR PROGRAM on RPi: 
 */
int led1 = 13; // led is connected to digital pin 13 
int led2 = 12;

void setup() { 
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT);
  Serial.begin(9600); // set the baud rate
  Serial.println("Ready"); // print "Ready" once
}

void loop() {
  char inByte = ' '; if(Serial.available()) { // only send data back if data has been sent
  char inByte = Serial.read();  // read the incoming data
  light_leds(inByte);
  Serial.println(inByte);  // send the data back in a new line so that it is $
                             // all one long line
  }
  else if(!Serial.available()){
    stop_go();
  }
  delay(100);
}

void light_leds(int n) {
  for(int i = 0; i < n; i++) {
    digitalWrite(led2, HIGH);
    Serial.println("inside light_leds");
    Serial.println(char(i+48));
    delay(500);
    digitalWrite(led2, LOW);
    delay(500);
  }
  Serial.println("light_leds END");
  delay(3000);
  Serial.println("going to stop_go");
  stop_go();
}

void stop_go() {  // does nothing at this point
}

No comments:

Post a Comment