Convert uno code to work on chipkit lenny

Post Reply
sternfox
Posts: 4
Joined: Tue Nov 14, 2017 11:25 pm

Convert uno code to work on chipkit lenny

Post by sternfox » Tue Nov 21, 2017 7:33 pm

Hi Guys, I have the following code working on my uno but need the extra pulse rate from the Lenny. Could someone please help me port this code to work on the lenny?

Code: Select all

#include <AccelStepper.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
int bluetoothTx = 0; // bluetooth TX to pin 0
int bluetoothRx = 1; // bluetooth rx to pin 1
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

AccelStepper stepper(1, 5, 6);  // 1 = Easy Driver interface, Pin 4 connected to STEP pin of Easy Driver, Pin 5 connected to DIR pin of Easy Driver

int change = 0;
int stepper_distance; 

void setup() {
  Serial.begin(9600);
  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);  
}//--(end setup )---

void loop() 
{
  if(bluetooth.available()> 0) // receive number from bluetooth
  {
    start:  
    int command = bluetooth.read(); // save the received number to command
    Serial.println(command);  //Printing on the Serial Monitor

    if (command >1 && command <=60)
    {
      int stepper_speed = map(command, 1, 60, 0, 3000);
      stepper.setMaxSpeed(stepper_speed);
    }

    else if (command >60 && command <=120)
    {
      int stepper_acceleration = map(command, 61, 120, 0, 30000);
      stepper.setAcceleration(stepper_acceleration);
    }
    
    else if (command >121 && command <=180)
    {
      stepper_distance = map(command, 121, 180, 0, 200);
    }
    
    else if (command == 1)
    {
      stepper.moveTo(0);
      while (stepper.currentPosition() != 0) // Full speed basck to 0
      {
       stepper.run();
       }
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      while(bluetooth.available() == 0)
        {
        digitalWrite(9, HIGH);
        digitalWrite(10, HIGH);
        }
    }
  }
   
  stepper.moveTo(stepper_distance);
  while (stepper.currentPosition() != stepper_distance) 
    {
      stepper.run();
      if(bluetooth.available()> 0) // receive number from bluetooth
    {
      goto start;
    }
    }  

  stepper.moveTo(0);
  while (stepper.currentPosition() != 0) // Full speed basck to 0
   {
    stepper.run();
    
   }
}

User avatar
majenko
Site Admin
Posts: 2179
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Convert uno code to work on chipkit lenny

Post by majenko » Tue Nov 21, 2017 11:39 pm

Which part doesn't work?

The main thing I would certainly change is to drop SoftwareSerial and use `Serial0` instead - it's the hardware UART on the TX and RX pins.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply